pub struct Machine {Show 16 fields
pub reg_a: u8,
pub reg_b: i16,
pub reg_L: u16,
pub reg_f: f64,
pub reg_ch: char,
pub reg_ř: [i8; 37],
pub reg_ß: ConstantSizeString,
pub reg_Ω: Ω,
pub num_reg: i32,
pub reg_ep: u16,
pub reg_dp: u16,
pub flag: bool,
pub debug_mode: bool,
pub halted: bool,
pub memory: Box<[u8; 65535]>,
pub stack: Stack,
}Expand description
An esoteric virtual machine.
Create a new machine with [Machine::new] and load
machine code and data to it with Machine::load.
§Examples
// initialize a new machine
let mut machine = Machine::default();
// assembly code for the machine
let asm = esoteric_assembly! {
// initialize dot pointer so that IO operations work
// push a dot character to stack
0: pushi b'.';
// pop to address 28657
2: pop 28657;
// set dot pointer to 28657 (has to be a prime or semiprime, which is also a fibonacci number)
5: ldidp 28657;
// -----------------
// print hello world
8: writeline 13;
// halt machine
11: Ωtheendisnear;
12: Ωskiptothechase;
// hello world text
13: data b"Hello, world!\n\0";
};
// load machine code
machine.load(&asm, 0);
// run machine until it halts
machine.run();
// return the machine's register A (unused)
machineFields§
§reg_a: u8register a (used as the machine’s exit code)
reg_b: i16register b
reg_L: u16register L
reg_f: f64register f
reg_ch: charregister ch (ch is one letter in Czech, therefore it’s valid)
reg_ř: [i8; 37]register ř
reg_ß: ConstantSizeStringregister ß
reg_Ω: Ωregister Ω
num_reg: i32number register (serves as the return value of the main function and
is printed in debug mode if reg_Ω.should_make_infinite_paperclips is true)
reg_ep: u16execution pointer
reg_dp: u16dot pointer (has to point to a . character. if it doesn’t, then IO operation attempts are cancelled and the flag is set)
flag: booloverflow/error flag
debug_mode: booldebug mode
halted: boolwhether the machine is halted (can’t run anymore and is finished)
memory: Box<[u8; 65535]>memory (should be 65K)
stack: Stackstack memory (default is 4K)
Implementations§
Source§impl Machine
impl Machine
Sourcepub fn fetch_byte(&mut self) -> u8
pub fn fetch_byte(&mut self) -> u8
Fetches a byte at [reg_ep] and increments [reg_ep] by 1.
Sourcepub fn fetch_2_bytes(&mut self) -> u16
pub fn fetch_2_bytes(&mut self) -> u16
Fetches 2 bytes at [reg_ep] as a big endian integer
and increments [reg_ep] by 2.
Sourcepub fn fetch_4_bytes(&mut self) -> u32
pub fn fetch_4_bytes(&mut self) -> u32
Fetches 4 bytes at [reg_ep] as a big endian integer
and increments [reg_ep] by 4.
Sourcepub fn fetch_8_bytes(&mut self) -> u64
pub fn fetch_8_bytes(&mut self) -> u64
Fetches 8 bytes at [reg_ep] as a big endian integer
and increments [reg_ep] by 8.
Sourcepub fn fetch_instruction_kind(&mut self) -> Option<InstructionKind>
pub fn fetch_instruction_kind(&mut self) -> Option<InstructionKind>
Fetches a byte and tries to turn it into an InstructionKind.
For more info, read the docs for [fetch_byte].
Sourcepub fn num_debug(&self)
pub fn num_debug(&self)
Prints [num_reg] with a colon and a space after it
if [reg_Ω.should_make_infinite_paperclips] is enabled.
Sourcepub fn fetch_instruction(&mut self) -> Option<Instruction>
pub fn fetch_instruction(&mut self) -> Option<Instruction>
Fetches an instruction from memory,
incrementing [reg_ep] based on the amount of bytes read.
Returns None if the machine is halted.
Sourcepub fn execute_instruction(&mut self, instruction: Instruction)
pub fn execute_instruction(&mut self, instruction: Instruction)
Fetches and executes an instruction.
More info at [fetch_instruction].
Sourcepub fn load_instructions(
&mut self,
instructions: &[Instruction],
offset: u16,
) -> u16
pub fn load_instructions( &mut self, instructions: &[Instruction], offset: u16, ) -> u16
Loads instructions into the machine’s memory at the specified offset.
Returns the amount of bytes written
Sourcepub fn load(&mut self, data: &[DataOrInstruction<'_>], offset: u16) -> u16
pub fn load(&mut self, data: &[DataOrInstruction<'_>], offset: u16) -> u16
Loads data into the machine’s memory at the specified offset.
Returns the amount of bytes written
Sourcepub fn load_bytes(&mut self, bytes: &[u8], offset: u16) -> Option<u16>
pub fn load_bytes(&mut self, bytes: &[u8], offset: u16) -> Option<u16>
Load bytes into the machine at the specified offset.
Returns the amount of bytes written
Sourcepub fn load_instruction(&mut self, instruction: Instruction, offset: &mut u16)
pub fn load_instruction(&mut self, instruction: Instruction, offset: &mut u16)
Loads a single instruction into memory at the specified offset, mutating it based on the amount of bytes written.