Module Cpu.Processor

processor.ml - CPU logic

type processor = {
  1. regs : Registers.registers;
  2. mem : Memory.Vmem.mem_ctrl;
  3. ih : Interrupts.interrupt_handler;
  4. halted : bool Stdlib.ref;
}

Emulates CPU state

val _init_processor : Memory.Vmem.mem_ctrl -> processor

Returns an initialized CPU state

val read_byte : processor -> char

Read the next byte at position (PC) and increment PC

val read_word : processor -> int

Read the next word (2 bytes) at position (PC) and increment PC twice

val is_flag_set : processor -> int -> bool

Check if a specific flag is set in the F register

val is_flag_set_b : processor -> int -> int

Check if a specific flag is set in the F register - returns an int

val set_flag : processor -> int -> bool -> unit

Set a flag in the F register

val rp_table_read : int -> Registers.registers -> Utils.U16.u16

Get the register read function corresponding to rpn

val rp_table_write : int -> Registers.registers -> int -> unit

Get the register write function corresponding to rpn

val r_table_read : processor -> int -> int * (Registers.registers -> Utils.U8.u8)

Get the register read function corresponding to r[n]

val r_table_write : processor -> int -> char -> unit

Get the register write function corresponding to rn

val push : processor -> int -> unit

Push 16 bits onto the stack

val push_rp2 : processor -> int -> unit

Push 16 bits onto the stack from a set register in the RP2 table

val pop : processor -> int

Pop 16 bits from the stack

val pop_rp2 : processor -> int -> unit

Pop 16 bits from the stack and load them into a set register in the RP2 table

val nop : unit -> int

No-operation

val stop : 'a -> int
val halt : processor -> int

Halts the CPU

val jp : processor -> int -> unit

Unconditional jump

val jr : processor -> int -> unit

Unconditional relative jump

val jp_cond : processor -> int -> int -> int

Conditional jump

jp_cond cpu cond address

val jr_cond : processor -> int -> int -> int

Conditional relative jump

jr_cond cpu cond offset

val call : processor -> int -> unit

Call

call cpu new_pc

val call_cond : processor -> int -> int -> int

Conditional call

call cpu new_pc cond

val ret : processor -> unit

Return

val ret_cond : processor -> int -> int

Conditional return

val rst : processor -> int -> int

Restart

val add16 : int Stdlib.ref -> int -> unit

(dest += src)%u16

val alu : processor -> int -> Utils.U8.u8 -> int

ALU table

alu cpu table_idx operand

val rotate_left : processor -> char -> bool -> bool -> char

Leftward bit-rotation operation

rotate_left cpu to_rotate include_carry update_zero

val rotate_right : processor -> char -> bool -> bool -> char

Rightward bit-rotation operation

rotate_right cpu to_rotate include_carry update_zero

val shift_left : processor -> char -> char

Left bitshift

shift_left cpu to_shift

val shift_right : processor -> char -> bool -> char

Right bitshift

shift_left cpu to_shift keep_msb

val swap : processor -> char -> char

Swaps the highest MSBs and lowest MSBs

swap cpu to_swap

val bit : processor -> char -> int -> unit

Test a bit bit cpu value bit_to_test

val set : char -> int -> char

Set a bit (just value | bit)

set cpu value bit

val res : char -> int -> char

Reset a bit

res cpu value bit

val rot : processor -> int -> int -> int

Rotation table ROT

val decode_execute : processor -> char -> int

General decoder

Layout is based on this table

Returns the number of cycles taken to execute the provided instruction

val de_step : processor -> int

Performs one step of the decode-execute cycle