iota-machine
|
#include <iota-machine.h>
Classes | |
struct | opcodeDetails |
Public Types | |
enum | opcodeNumber { OP_NOP, OP_RESET, OP_HALT, OP_IN, OP_OUT, OP_POP, OP_DUP, OP_PUSHPC, OP_POPPC, OP_POPSP, OP_SPTGT, OP_PUSHNZ, OP_SWAP, OP_PUSH0, OP_ADD, OP_SUB, OP_INC, OP_DEC, OP_MUL, OP_DIV, OP_XOR, OP_AND, OP_OR, OP_SHL, OP_SHR, OP_NOT, OP_BZ, OP_BNZ, OP_BEQ, OP_BGT, OP_BLT, OP_BGE, OP_LOOP, OP_ENDL, OP_BRAN, OP_BRAP, OP_TARGET, OP_SKIP1, OP_SKIP2, OP_SKIP3, OP_SKIP4, OP_SKIP5, OP_SKIP6, OP_SKIP7, OP_SKIP8, OP_SKIP9, NUMBER_OF_OPCODES } |
typedef T | value_type |
Exposes the template param, an unsigned integral type. | |
Public Member Functions | |
machine (size_t archLength, size_t archWidth=8) | |
Construct a virtual machine with memory archLength long and archWidth wide. | |
void | reset (void) |
Reset the virtual machine: PC = SP = 0; NZ = false, clear output string. | |
void | copyProgramToMachineMemory (const std::string &src) |
Given a source string of single-char mnemonics, copy to Iota memory. | |
void | setDataInputSource (const std::string &dataInSource) |
Define a string of chars to supply the IN opcode. | |
void | setDebugLevel (int level) |
Zero for no debug output; nonzero gives additional output. | |
void | setExecutionTrace (int level) |
Any nonzero value enables Iota execution trace to stdout. | |
std::string | listProgramShortMnemonics (size_t maxLength=0) const |
List the Iota program current in memory. size 0 means archLength. | |
void | sprintState (void) const |
Print memory and register contents. | |
void | disassemble (void) const |
Generate a listing with long mnemonics of the Iota program in memory. | |
size_t | executeAtMost (size_t numberOfCycles) |
Execute the Iota program until it encounters HALT or executes this many cycles. | |
bool | executeOneCycle (void) |
Execute the next Iota instruction. | |
Public Attributes | |
std::string | mOutputStream |
const size_t | maxOutputStreamSize = 128*1024 |
Some reasonably big limit for mOutputStream . | |
std::string | allOpcodeChars |
An instance of this class simulates an Iota processing machine.
T | The unsigned type that will be the underlying representation for Iota memory locations. It must be an unsigned type at least archWidth bits wide. |
Definition at line 74 of file iota-machine.h.
enum iota::machine::opcodeNumber |
The master list of Iota opcode numbers. Determines the opcode numbers only.
Definition at line 138 of file iota-machine.h.
iota::machine< T >::machine | ( | size_t | archLength, |
size_t | archWidth = 8 |
||
) |
Construct a virtual machine with memory archLength long and archWidth wide.
Constructs an Iota virtual machine.
[in] | archWidth | The width in bits of the Iota memory system, in the range [6..32). |
[in] | archLength | The number of memory locations in the Iota virtual machine, in the range [1..2^32). |
iotaExcept | Throws iota::iotaExcept if any ctor argument is out of range. |
Definition at line 315 of file iota-machine.h.
void iota::machine< T >::copyProgramToMachineMemory | ( | const std::string & | src | ) |
Given a source string of single-char mnemonics, copy to Iota memory.
Given a string of characters, regard it as the source for an Iota program and copy it to the Iota simulated memory starting at location zero. Characters that are valid opcodes are stored in Iota memory as the opcode number, otherwise the char value is cast to type T
and stored.
[in] | src | An Iota source program, one character per opcode. If the string is longer than Iota memory, the remainder of the string that doesn't fit is ignored. |
Definition at line 365 of file iota-machine.h.
void iota::machine< T >::disassemble | ( | void | ) | const |
Generate a listing with long mnemonics of the Iota program in memory.
Print to std::cerr (stderr) a disassembly, using long opcode mnemonics, of the Iota simulated memory and registers.
Definition at line 484 of file iota-machine.h.
size_t iota::machine< T >::executeAtMost | ( | size_t | numberOfCycles | ) |
Execute the Iota program until it encounters HALT or executes this many cycles.
A wrapper around executeOneCycle()
.
[in] | numberOfCycles | The maximum number of times to call executeOneCycle() . The Iota program will terminate prior to that if it executes a HALT instruction. |
executeOneCycle()
Definition at line 514 of file iota-machine.h.
bool iota::machine< T >::executeOneCycle | ( | void | ) |
Execute the next Iota instruction.
Executes the next Iota instruction. Depends on the current state of Iota memory and the simulated registers PC
, SP
, and NZ
.
Definition at line 585 of file iota-machine.h.
std::string iota::machine< T >::listProgramShortMnemonics | ( | size_t | maxLength = 0 | ) | const |
List the Iota program current in memory. size 0 means archLength.
Generate a listing of the Iota program currently in memory. Memory locations that contain a valid opcode number are represented by its single-character opcode mnemonic, otherwise the value in memory is cast to a char
and represented by a single character.
[in] | maxLength | If zero (the default), the entire contents of Iota memory will be formatted in source form, otherwise maxLength specifies the number of memory locations to list in source form, starting at location zero. |
Definition at line 429 of file iota-machine.h.
void iota::machine< T >::reset | ( | void | ) |
Reset the virtual machine: PC = SP = 0; NZ = false, clear output string.
Reset the Iota virtual machine. This sets the PC
and SP
to location zero, clears the NZ
flag, resets the execution cycle counter to zero, clears the output stream, and clears simulated memory to all zero (OP_NOP opcodes)
Definition at line 343 of file iota-machine.h.
void iota::machine< T >::setDataInputSource | ( | const std::string & | dataInSource | ) |
Define a string of chars to supply the IN opcode.
This specifies a string (copied into the Iota machine) that will supply the stream of characters to be consumed by the Iota IN
opcode, one character from dataInSource
for each IN
instruction. When the input reaches the end of the string, subsequent IN
instructions will repeatedly retrieve the last character of the input string as though the input was an infinitely long string.
[in] | dataInSource | A string that will supply characters for the Iota IN instruction. |
Definition at line 390 of file iota-machine.h.
void iota::machine< T >::setDebugLevel | ( | int | level | ) |
Zero for no debug output; nonzero gives additional output.
Setting a nonzero debug level will cause extra output to be sent to std::cerr (stderr).
[in] | level | Zero for no debug output; nonzero for more. |
Definition at line 402 of file iota-machine.h.
void iota::machine< T >::setExecutionTrace | ( | int | level | ) |
Any nonzero value enables Iota execution trace to stdout.
A nonzero level causes a line to be printed to std::cerr (stderr) for each Iota opcode executed.
[in] | level | Zero for no trace; nonzero to enable execution trace. |
Definition at line 413 of file iota-machine.h.
void iota::machine< T >::sprintState | ( | void | ) | const |
Print memory and register contents.
Print to std::cerr (stderr) the state of Iota memory and registers, and a one-line disassembly of the next Iota opcode to be executed.
Definition at line 457 of file iota-machine.h.
std::string iota::machine< T >::allOpcodeChars |
At runtime, a list of all the single-character Iota opcode mnemonics is collected in this container, derived from the master opcode table opcodeTable
.
Definition at line 132 of file iota-machine.h.
std::string iota::machine< T >::mOutputStream |
The container that collects the characters output by the Iota OUT
opcode. (An alternative implementation could have been made using an ostringstream
or ostream
reference, where the caller owns the container.) The size of the string is capped at machine::maxOutputStreamSize
characters.
Definition at line 121 of file iota-machine.h.