ducky.cpu.coprocessor.math_copro module

Stack-based coprocessor, providing several arithmetic operations with “long” numbers.

Coprocessor’s instructions operates on a stack of (by default) 8 slots. Operations to move values between math stack and registers/data stack are also available.

In the following documentation several different data types are used:

  • int - standard word, 32-bit wide integer
  • long - long integer, 64-bit wide

Unless said otherwise, instruction takes its arguments from the stack, removing the values in the process, and pushes the result - if any - back on the stack.

class ducky.cpu.coprocessor.math_copro.ADDL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

static execute(core, inst)[source]
mnemonic = 'addl'
opcode = 32
operands = ['']
class ducky.cpu.coprocessor.math_copro.DECL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Decrement top of the stack by one.

static execute(core, inst)[source]
mnemonic = 'decl'
opcode = 31
operands = ['']
class ducky.cpu.coprocessor.math_copro.DIVL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Divide the value below the top of the math stack by the topmost value.

static execute(core, inst)[source]
mnemonic = 'divl'
opcode = 11
operands = ['']
class ducky.cpu.coprocessor.math_copro.DROP(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

static execute(core, inst)[source]
mnemonic = 'drop'
opcode = 23
operands = ['']
class ducky.cpu.coprocessor.math_copro.DUP(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

static execute(core, inst)[source]
mnemonic = 'dup'
opcode = 20
operands = ['']
class ducky.cpu.coprocessor.math_copro.DUP2(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

static execute(core, inst)[source]
mnemonic = 'dup2'
opcode = 21
operands = ['']
class ducky.cpu.coprocessor.math_copro.Descriptor_MATH(instruction_set)[source]

Bases: ducky.cpu.instructions.Descriptor_R_R

static assemble_operands(ctx, inst, operands)[source]
static disassemble_operands(logger, inst)[source]
operands = ''
exception ducky.cpu.coprocessor.math_copro.EmptyMathStackError(*args, **kwargs)[source]

Bases: ducky.errors.CoprocessorError

Raised when operation expects at least one value on math stack but stack is empty.

exception ducky.cpu.coprocessor.math_copro.FullMathStackError(*args, **kwargs)[source]

Bases: ducky.errors.CoprocessorError

Raised when operation tries to put value on math stack but there is no empty spot available.

class ducky.cpu.coprocessor.math_copro.INCL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Increment top of the stack by one.

static execute(core, inst)[source]
mnemonic = 'incl'
opcode = 30
operands = ['']
class ducky.cpu.coprocessor.math_copro.LOAD(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Merge two registers together, and make the result new TOS.

static assemble_operands(ctx, inst, operands)[source]
static disassemble_operands(logger, inst)[source]
static execute(core, inst)[source]
mnemonic = 'load'
opcode = 9
operands = ['r', 'r']
class ducky.cpu.coprocessor.math_copro.LOADUW(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Take a value from register, extend it to long, and make the result TOS.

static assemble_operands(ctx, inst, operands)[source]
static disassemble_operands(logger, inst)[source]
static execute(core, inst)[source]
static jit(core, inst)[source]
mnemonic = 'loaduw'
opcode = 5
operands = ['r']
class ducky.cpu.coprocessor.math_copro.LOADW(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Take a value from register, extend it to long, and make the result TOS.

static assemble_operands(ctx, inst, operands)[source]
static disassemble_operands(logger, inst)[source]
static execute(core, inst)[source]
static jit(core, inst)[source]
mnemonic = 'loadw'
opcode = 4
operands = ['r']
class ducky.cpu.coprocessor.math_copro.MODL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

static execute(core, inst)[source]
mnemonic = 'modl'
opcode = 12
operands = ['']
class ducky.cpu.coprocessor.math_copro.MULL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Multiply two top-most numbers on the stack.

static execute(core, inst)[source]
static jit(core, inst)[source]
mnemonic = 'mull'
opcode = 10
operands = ['']
class ducky.cpu.coprocessor.math_copro.MathCoprocessor(core, *args, **kwargs)[source]

Bases: ducky.interfaces.ISnapshotable, ducky.cpu.coprocessor.Coprocessor

Coprocessor itself, includes its register set (“math stack”).

Parameters:core (ducky.cpu.CPUCore) – CPU core coprocessor belongs to.
dump_stack()[source]

Log content of the stack using parent’s DEBUG method.

extend_with_push(u32)[source]
load_state(state)[source]
save_state(parent)[source]
sign_extend_with_push(i32)[source]
class ducky.cpu.coprocessor.math_copro.MathCoprocessorInstructionSet[source]

Bases: ducky.cpu.instructions.InstructionSet

Math coprocessor’s instruction set.

instruction_set_id = 1
instructions = [<ducky.cpu.coprocessor.math_copro.ADDL object>, <ducky.cpu.coprocessor.math_copro.INCL object>, <ducky.cpu.coprocessor.math_copro.DECL object>, <ducky.cpu.coprocessor.math_copro.MULL object>, <ducky.cpu.coprocessor.math_copro.DIVL object>, <ducky.cpu.coprocessor.math_copro.MODL object>, <ducky.cpu.coprocessor.math_copro.UDIVL object>, <ducky.cpu.coprocessor.math_copro.UMODL object>, <ducky.cpu.coprocessor.math_copro.SYMDIVL object>, <ducky.cpu.coprocessor.math_copro.SYMMODL object>, <ducky.cpu.coprocessor.math_copro.DUP object>, <ducky.cpu.coprocessor.math_copro.DUP2 object>, <ducky.cpu.coprocessor.math_copro.SWP object>, <ducky.cpu.coprocessor.math_copro.DROP object>, <ducky.cpu.coprocessor.math_copro.PUSHW object>, <ducky.cpu.coprocessor.math_copro.SAVEW object>, <ducky.cpu.coprocessor.math_copro.POPW object>, <ducky.cpu.coprocessor.math_copro.LOADW object>, <ducky.cpu.coprocessor.math_copro.POPUW object>, <ducky.cpu.coprocessor.math_copro.LOADUW object>, <ducky.cpu.coprocessor.math_copro.PUSHL object>, <ducky.cpu.coprocessor.math_copro.SAVE object>, <ducky.cpu.coprocessor.math_copro.POPL object>, <ducky.cpu.coprocessor.math_copro.LOAD object>, <ducky.cpu.instructions.SIS object>]
opcode_desc_map = {<MathCoprocessorOpcodes.POPW: 0>: <ducky.cpu.coprocessor.math_copro.POPW object>, <MathCoprocessorOpcodes.POPUW: 1>: <ducky.cpu.coprocessor.math_copro.POPUW object>, <MathCoprocessorOpcodes.PUSHW: 2>: <ducky.cpu.coprocessor.math_copro.PUSHW object>, <MathCoprocessorOpcodes.SAVEW: 3>: <ducky.cpu.coprocessor.math_copro.SAVEW object>, <MathCoprocessorOpcodes.LOADW: 4>: <ducky.cpu.coprocessor.math_copro.LOADW object>, <MathCoprocessorOpcodes.LOADUW: 5>: <ducky.cpu.coprocessor.math_copro.LOADUW object>, <MathCoprocessorOpcodes.POPL: 6>: <ducky.cpu.coprocessor.math_copro.POPL object>, <MathCoprocessorOpcodes.SAVE: 7>: <ducky.cpu.coprocessor.math_copro.SAVE object>, <MathCoprocessorOpcodes.PUSHL: 8>: <ducky.cpu.coprocessor.math_copro.PUSHL object>, <MathCoprocessorOpcodes.LOAD: 9>: <ducky.cpu.coprocessor.math_copro.LOAD object>, <MathCoprocessorOpcodes.MULL: 10>: <ducky.cpu.coprocessor.math_copro.MULL object>, <MathCoprocessorOpcodes.DIVL: 11>: <ducky.cpu.coprocessor.math_copro.DIVL object>, <MathCoprocessorOpcodes.MODL: 12>: <ducky.cpu.coprocessor.math_copro.MODL object>, <MathCoprocessorOpcodes.SYMDIVL: 13>: <ducky.cpu.coprocessor.math_copro.SYMDIVL object>, <MathCoprocessorOpcodes.SYMMODL: 14>: <ducky.cpu.coprocessor.math_copro.SYMMODL object>, <MathCoprocessorOpcodes.UDIVL: 15>: <ducky.cpu.coprocessor.math_copro.UDIVL object>, <MathCoprocessorOpcodes.UMODL: 16>: <ducky.cpu.coprocessor.math_copro.UMODL object>, <MathCoprocessorOpcodes.DUP: 20>: <ducky.cpu.coprocessor.math_copro.DUP object>, <MathCoprocessorOpcodes.DUP2: 21>: <ducky.cpu.coprocessor.math_copro.DUP2 object>, <MathCoprocessorOpcodes.SWP: 22>: <ducky.cpu.coprocessor.math_copro.SWP object>, <MathCoprocessorOpcodes.DROP: 23>: <ducky.cpu.coprocessor.math_copro.DROP object>, <MathCoprocessorOpcodes.INCL: 30>: <ducky.cpu.coprocessor.math_copro.INCL object>, <MathCoprocessorOpcodes.DECL: 31>: <ducky.cpu.coprocessor.math_copro.DECL object>, <MathCoprocessorOpcodes.ADDL: 32>: <ducky.cpu.coprocessor.math_copro.ADDL object>, <DuckyOpcodes.SIS: 63>: <ducky.cpu.instructions.SIS object>}
opcode_encoding_map = {<MathCoprocessorOpcodes.POPW: 0>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.POPUW: 1>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.PUSHW: 2>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.SAVEW: 3>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.LOADW: 4>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.LOADUW: 5>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.POPL: 6>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.SAVE: 7>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.PUSHL: 8>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.LOAD: 9>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.MULL: 10>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.DIVL: 11>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.MODL: 12>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.SYMDIVL: 13>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.SYMMODL: 14>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.UDIVL: 15>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.UMODL: 16>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.DUP: 20>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.DUP2: 21>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.SWP: 22>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.DROP: 23>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.INCL: 30>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.DECL: 31>: <class 'ducky.cpu.instructions.EncodingR'>, <MathCoprocessorOpcodes.ADDL: 32>: <class 'ducky.cpu.instructions.EncodingR'>, <DuckyOpcodes.SIS: 63>: <class 'ducky.cpu.instructions.EncodingI'>}
opcodes

alias of MathCoprocessorOpcodes

class ducky.cpu.coprocessor.math_copro.MathCoprocessorOpcodes[source]

Bases: enum.IntEnum

Math coprocessor’s instruction opcodes.

ADDL = 32
DECL = 31
DIVL = 11
DROP = 23
DUP = 20
DUP2 = 21
INCL = 30
LOAD = 9
LOADUW = 5
LOADW = 4
MODL = 12
MULL = 10
POPL = 6
POPUW = 1
POPW = 0
PUSHL = 8
PUSHW = 2
SAVE = 7
SAVEW = 3
SIS = 63
SWP = 22
SYMDIVL = 13
SYMMODL = 14
UDIVL = 15
UMODL = 16
_member_map_ = OrderedDict([('POPW', <MathCoprocessorOpcodes.POPW: 0>), ('POPUW', <MathCoprocessorOpcodes.POPUW: 1>), ('PUSHW', <MathCoprocessorOpcodes.PUSHW: 2>), ('SAVEW', <MathCoprocessorOpcodes.SAVEW: 3>), ('LOADW', <MathCoprocessorOpcodes.LOADW: 4>), ('LOADUW', <MathCoprocessorOpcodes.LOADUW: 5>), ('POPL', <MathCoprocessorOpcodes.POPL: 6>), ('SAVE', <MathCoprocessorOpcodes.SAVE: 7>), ('PUSHL', <MathCoprocessorOpcodes.PUSHL: 8>), ('LOAD', <MathCoprocessorOpcodes.LOAD: 9>), ('MULL', <MathCoprocessorOpcodes.MULL: 10>), ('DIVL', <MathCoprocessorOpcodes.DIVL: 11>), ('MODL', <MathCoprocessorOpcodes.MODL: 12>), ('SYMDIVL', <MathCoprocessorOpcodes.SYMDIVL: 13>), ('SYMMODL', <MathCoprocessorOpcodes.SYMMODL: 14>), ('UDIVL', <MathCoprocessorOpcodes.UDIVL: 15>), ('UMODL', <MathCoprocessorOpcodes.UMODL: 16>), ('DUP', <MathCoprocessorOpcodes.DUP: 20>), ('DUP2', <MathCoprocessorOpcodes.DUP2: 21>), ('SWP', <MathCoprocessorOpcodes.SWP: 22>), ('DROP', <MathCoprocessorOpcodes.DROP: 23>), ('INCL', <MathCoprocessorOpcodes.INCL: 30>), ('DECL', <MathCoprocessorOpcodes.DECL: 31>), ('ADDL', <MathCoprocessorOpcodes.ADDL: 32>), ('SIS', <MathCoprocessorOpcodes.SIS: 63>)])
_member_names_ = ['POPW', 'POPUW', 'PUSHW', 'SAVEW', 'LOADW', 'LOADUW', 'POPL', 'SAVE', 'PUSHL', 'LOAD', 'MULL', 'DIVL', 'MODL', 'SYMDIVL', 'SYMMODL', 'UDIVL', 'UMODL', 'DUP', 'DUP2', 'SWP', 'DROP', 'INCL', 'DECL', 'ADDL', 'SIS']
_member_type_

alias of int

_value2member_map_ = {0: <MathCoprocessorOpcodes.POPW: 0>, 1: <MathCoprocessorOpcodes.POPUW: 1>, 2: <MathCoprocessorOpcodes.PUSHW: 2>, 3: <MathCoprocessorOpcodes.SAVEW: 3>, 4: <MathCoprocessorOpcodes.LOADW: 4>, 5: <MathCoprocessorOpcodes.LOADUW: 5>, 6: <MathCoprocessorOpcodes.POPL: 6>, 7: <MathCoprocessorOpcodes.SAVE: 7>, 8: <MathCoprocessorOpcodes.PUSHL: 8>, 9: <MathCoprocessorOpcodes.LOAD: 9>, 10: <MathCoprocessorOpcodes.MULL: 10>, 11: <MathCoprocessorOpcodes.DIVL: 11>, 12: <MathCoprocessorOpcodes.MODL: 12>, 13: <MathCoprocessorOpcodes.SYMDIVL: 13>, 14: <MathCoprocessorOpcodes.SYMMODL: 14>, 15: <MathCoprocessorOpcodes.UDIVL: 15>, 16: <MathCoprocessorOpcodes.UMODL: 16>, 20: <MathCoprocessorOpcodes.DUP: 20>, 21: <MathCoprocessorOpcodes.DUP2: 21>, 22: <MathCoprocessorOpcodes.SWP: 22>, 23: <MathCoprocessorOpcodes.DROP: 23>, 30: <MathCoprocessorOpcodes.INCL: 30>, 31: <MathCoprocessorOpcodes.DECL: 31>, 32: <MathCoprocessorOpcodes.ADDL: 32>, 63: <MathCoprocessorOpcodes.SIS: 63>}
class ducky.cpu.coprocessor.math_copro.MathCoprocessorState[source]

Bases: ducky.snapshot.SnapshotNode

Snapshot node holding the state of math coprocessor.

class ducky.cpu.coprocessor.math_copro.POPL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Pop the long from data stack, and make it new TOS.

static execute(core, inst)[source]
mnemonic = 'popl'
opcode = 6
operands = ['']
class ducky.cpu.coprocessor.math_copro.POPUW(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Pop the int``from data stack, extend it to ``long, and make the result TOS.

static execute(core, inst)[source]
mnemonic = 'popuw'
opcode = 1
operands = ['']
class ducky.cpu.coprocessor.math_copro.POPW(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Pop the int from data stack, extend it to long, and make the result TOS.

static execute(core, inst)[source]
static jit(core, inst)[source]
mnemonic = 'popw'
opcode = 0
operands = ['']
class ducky.cpu.coprocessor.math_copro.PUSHL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Push the TOS on the data stack.

static execute(core, inst)[source]
mnemonic = 'pushl'
opcode = 8
operands = ['']
class ducky.cpu.coprocessor.math_copro.PUSHW(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Downsize TOS to int, and push the result on the data stack.

static execute(core, inst)[source]
mnemonic = 'pushw'
opcode = 2
operands = ['']
class ducky.cpu.coprocessor.math_copro.RegisterSet(core)[source]

Bases: ducky.interfaces.ISnapshotable

Math stack wrapping class. Provides basic push/pop access, and direct access to a top of the stack.

Parameters:core (ducky.cpu.CPUCore) – CPU core registers belong to.
load_state(state)[source]
pop()[source]

Pop the top value from stack and return it.

Raises:ducky.cpu.coprocessor.math_copro.EmptyMathStackError – if there are no values on the stack.
push(v)[source]

Push new value on top of the stack.

Raises:ducky.cpu.coprocessor.math_copro.FullMathStackError – if there is no space available on the stack.
save_state(parent)[source]
tos()[source]

Return the top of the stack, without removing it from a stack.

Raises:ducky.cpu.coprocessor.math_copro.EmptyMathStackError – if there are no values on the stack.
tos1()[source]

Return the item below the top of the stack, without removing it from a stack.

Raises:ducky.cpu.coprocessor.math_copro.EmptyMathStackError – if there are no values on the stack.
class ducky.cpu.coprocessor.math_copro.SAVE(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Store TOS in two registers.

static assemble_operands(ctx, inst, operands)[source]
static disassemble_operands(logger, inst)[source]
static execute(core, inst)[source]
static jit(core, inst)[source]
mnemonic = 'save'
opcode = 7
operands = ['r', 'r']
class ducky.cpu.coprocessor.math_copro.SAVEW(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Downsize TOS to int, and store the result in register.

static assemble_operands(ctx, inst, operands)[source]
static disassemble_operands(logger, inst)[source]
static execute(core, inst)[source]
static jit(core, inst)[source]
mnemonic = 'savew'
opcode = 3
operands = ['r']
ducky.cpu.coprocessor.math_copro.STACK_DEPTH = 8

Number of available spots on the math stack.

class ducky.cpu.coprocessor.math_copro.SWP(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

static execute(core, inst)[source]
mnemonic = 'swpl'
opcode = 22
operands = ['']
class ducky.cpu.coprocessor.math_copro.SYMDIVL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

The same operation like DIVL but provides symmetric results.

static execute(core, inst)[source]
mnemonic = 'symdivl'
opcode = 13
operands = ['']
class ducky.cpu.coprocessor.math_copro.SYMMODL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

static execute(core, inst)[source]
mnemonic = 'symmodl'
opcode = 14
operands = ['']
class ducky.cpu.coprocessor.math_copro.UDIVL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

Divide the value below the top of the math stack by the topmost value.

static execute(core, inst)[source]
mnemonic = 'udivl'
opcode = 15
operands = ['']
class ducky.cpu.coprocessor.math_copro.UMODL(instruction_set)[source]

Bases: ducky.cpu.coprocessor.math_copro.Descriptor_MATH

static execute(core, inst)[source]
mnemonic = 'umodl'
opcode = 16
operands = ['']