CodeGen

Struct CodeGen 

Source
pub struct CodeGen { /* private fields */ }
Expand description

Core code generator

Implementations§

Source§

impl CodeGen

Source

pub fn new() -> Self

Create a new code generator with default config

Source

pub fn with_config(config: RomConfig) -> Self

Create a new code generator with custom config

Source

pub fn config(&self) -> &RomConfig

Get the ROM configuration

Source

pub fn pos(&self) -> u16

Get current emit position (address)

Source

pub fn size(&self) -> usize

Get current ROM size in bytes

Source

pub fn unique_label(&mut self, prefix: &str) -> String

Generate a unique label name

Source

pub fn emit(&mut self, bytes: &[u8])

Emit raw bytes

Source

pub fn emit_byte(&mut self, b: u8)

Emit a single byte

Source

pub fn emit_word(&mut self, word: u16)

Emit a 16-bit word (little-endian)

Source

pub fn emit_string(&mut self, s: &str)

Emit a null-terminated string

Source

pub fn emit_string_raw(&mut self, s: &str)

Emit a string without null terminator

Source

pub fn label(&mut self, name: &str)

Define a label at current position

Source

pub fn has_label(&self, name: &str) -> bool

Check if a label exists

Source

pub fn get_label(&self, name: &str) -> Option<u16>

Get label address (if defined)

Source

pub fn fixup(&mut self, name: &str)

Record a fixup for later resolution (emits placeholder word)

Source

pub fn resolve_fixups(&mut self)

Resolve all fixups - call after all code is emitted

Source

pub fn emit_relative(&mut self, target_label: &str)

Emit a relative jump offset (for JR, DJNZ) target_label must already be defined

Source

pub fn rom(&self) -> &[u8]

Get the raw ROM bytes

Source

pub fn rom_mut(&mut self) -> &mut Vec<u8>

Get mutable access to ROM bytes (for patching relative jumps, etc.)

Source

pub fn write_bin(&self, path: &str) -> Result<()>

Write ROM to binary file

Source

pub fn write_hex(&self, path: &str) -> Result<()>

Write ROM as Intel HEX format

Source§

impl CodeGen

Source

pub fn ld_a(&mut self, n: u8)

LD A, n

Source

pub fn ld_b(&mut self, n: u8)

LD B, n

Source

pub fn ld_c(&mut self, n: u8)

LD C, n

Source

pub fn ld_d(&mut self, n: u8)

LD D, n

Source

pub fn ld_e(&mut self, n: u8)

LD E, n

Source

pub fn ld_h(&mut self, n: u8)

LD H, n

Source

pub fn ld_l(&mut self, n: u8)

LD L, n

Source

pub fn ld_a_hl_ind(&mut self)

LD A, (HL)

Source

pub fn ld_hl_ind_a(&mut self)

LD (HL), A

Source

pub fn ld_a_b(&mut self)

LD A, B

Source

pub fn ld_a_c(&mut self)

LD A, C

Source

pub fn ld_a_d(&mut self)

LD A, D

Source

pub fn ld_a_e(&mut self)

LD A, E

Source

pub fn ld_b_a(&mut self)

LD B, A

Source

pub fn ld_c_a(&mut self)

LD C, A

Source

pub fn ld_d_a(&mut self)

LD D, A

Source

pub fn ld_e_a(&mut self)

LD E, A

Source

pub fn ld_a_addr(&mut self, addr: u16)

LD A, (nn)

Source

pub fn ld_addr_a(&mut self, addr: u16)

LD (nn), A

Source

pub fn ld_bc(&mut self, nn: u16)

LD BC, nn

Source

pub fn ld_de(&mut self, nn: u16)

LD DE, nn

Source

pub fn ld_hl(&mut self, nn: u16)

LD HL, nn

Source

pub fn ld_sp(&mut self, nn: u16)

LD SP, nn

Source

pub fn ld_hl_addr(&mut self, addr: u16)

LD HL, (nn)

Source

pub fn ld_addr_hl(&mut self, addr: u16)

LD (nn), HL

Source

pub fn ld_de_addr(&mut self, addr: u16)

LD DE, (nn) - ED instruction

Source

pub fn ld_addr_de(&mut self, addr: u16)

LD (nn), DE - ED instruction

Source

pub fn ld_sp_hl(&mut self)

LD SP, HL

Source

pub fn push_af(&mut self)

PUSH AF

Source

pub fn push_bc(&mut self)

PUSH BC

Source

pub fn push_de(&mut self)

PUSH DE

Source

pub fn push_hl(&mut self)

PUSH HL

Source

pub fn pop_af(&mut self)

POP AF

Source

pub fn pop_bc(&mut self)

POP BC

Source

pub fn pop_de(&mut self)

POP DE

Source

pub fn pop_hl(&mut self)

POP HL

Source

pub fn ex_de_hl(&mut self)

EX DE, HL

Source

pub fn ex_af(&mut self)

EX AF, AF’

Source

pub fn exx(&mut self)

EXX

Source

pub fn add_a(&mut self, n: u8)

ADD A, n

Source

pub fn add_a_b(&mut self)

ADD A, B

Source

pub fn add_a_hl_ind(&mut self)

ADD A, (HL)

Source

pub fn sub_a(&mut self, n: u8)

SUB n

Source

pub fn sub_b(&mut self)

SUB B

Source

pub fn inc_a(&mut self)

INC A

Source

pub fn inc_b(&mut self)

INC B

Source

pub fn inc_c(&mut self)

INC C

Source

pub fn dec_a(&mut self)

DEC A

Source

pub fn dec_b(&mut self)

DEC B

Source

pub fn dec_c(&mut self)

DEC C

Source

pub fn inc_hl(&mut self)

INC HL

Source

pub fn inc_de(&mut self)

INC DE

Source

pub fn inc_bc(&mut self)

INC BC

Source

pub fn dec_hl(&mut self)

DEC HL

Source

pub fn dec_de(&mut self)

DEC DE

Source

pub fn dec_bc(&mut self)

DEC BC

Source

pub fn add_hl_bc(&mut self)

ADD HL, BC

Source

pub fn add_hl_de(&mut self)

ADD HL, DE

Source

pub fn add_hl_hl(&mut self)

ADD HL, HL

Source

pub fn sbc_hl_de(&mut self)

SBC HL, DE

Source

pub fn sbc_hl_bc(&mut self)

SBC HL, BC

Source

pub fn and_a(&mut self, n: u8)

AND n

Source

pub fn or_a(&mut self, n: u8)

OR n

Source

pub fn or_a_a(&mut self)

OR A (common for flag check)

Source

pub fn or_b(&mut self)

OR B

Source

pub fn or_l(&mut self)

OR L

Source

pub fn xor_a(&mut self)

XOR A

Source

pub fn xor_n(&mut self, n: u8)

XOR n

Source

pub fn cp(&mut self, n: u8)

CP n

Source

pub fn cp_b(&mut self)

CP B

Source

pub fn cp_hl_ind(&mut self)

CP (HL)

Source

pub fn cpl(&mut self)

CPL (complement A)

Source

pub fn jp(&mut self, label: &str)

JP nn (with fixup)

Source

pub fn jp_addr(&mut self, addr: u16)

JP nn (absolute address)

Source

pub fn jp_z(&mut self, label: &str)

JP Z, nn

Source

pub fn jp_nz(&mut self, label: &str)

JP NZ, nn

Source

pub fn jp_c(&mut self, label: &str)

JP C, nn

Source

pub fn jp_nc(&mut self, label: &str)

JP NC, nn

Source

pub fn jp_p(&mut self, label: &str)

JP P, nn (positive/sign flag clear)

Source

pub fn jp_m(&mut self, label: &str)

JP M, nn (minus/sign flag set)

Source

pub fn jp_hl(&mut self)

JP (HL)

Source

pub fn jr(&mut self, label: &str)

JR e (relative jump, label must be defined)

Source

pub fn jr_z(&mut self, label: &str)

JR Z, e

Source

pub fn jr_nz(&mut self, label: &str)

JR NZ, e

Source

pub fn jr_c(&mut self, label: &str)

JR C, e

Source

pub fn jr_nc(&mut self, label: &str)

JR NC, e

Source

pub fn djnz(&mut self, label: &str)

DJNZ e (decrement B, jump if not zero)

Source

pub fn call(&mut self, label: &str)

CALL nn (with fixup)

Source

pub fn call_addr(&mut self, addr: u16)

CALL nn (absolute address)

Source

pub fn call_z(&mut self, label: &str)

CALL Z, nn

Source

pub fn call_nz(&mut self, label: &str)

CALL NZ, nn

Source

pub fn ret(&mut self)

RET

Source

pub fn ret_z(&mut self)

RET Z

Source

pub fn ret_nz(&mut self)

RET NZ

Source

pub fn ret_c(&mut self)

RET C

Source

pub fn ret_nc(&mut self)

RET NC

Source

pub fn in_a(&mut self, port: u8)

IN A, (n)

Source

pub fn out_a(&mut self, port: u8)

OUT (n), A

Source

pub fn nop(&mut self)

NOP

Source

pub fn halt(&mut self)

HALT

Source

pub fn di(&mut self)

DI (disable interrupts)

Source

pub fn ei(&mut self)

EI (enable interrupts)

Source

pub fn scf(&mut self)

SCF (set carry flag)

Source

pub fn ccf(&mut self)

CCF (complement carry flag)

Source

pub fn bit_a(&mut self, bit: u8)

BIT b, A

Source

pub fn set_a(&mut self, bit: u8)

SET b, A

Source

pub fn res_a(&mut self, bit: u8)

RES b, A

Source

pub fn rla(&mut self)

RLA (rotate left through carry)

Source

pub fn rra(&mut self)

RRA (rotate right through carry)

Source

pub fn rlca(&mut self)

RLCA (rotate left circular)

Source

pub fn rrca(&mut self)

RRCA (rotate right circular)

Source

pub fn sla_a(&mut self)

SLA A (shift left arithmetic)

Source

pub fn sra_a(&mut self)

SRA A (shift right arithmetic)

Source

pub fn srl_a(&mut self)

SRL A (shift right logical)

Source§

impl CodeGen

Source

pub fn emit_getchar(&mut self)

Emit getchar routine (blocking read, char returned in A)

Labels created: getchar

Source

pub fn emit_getchar_config(&mut self, config: &MC6850Config)

Emit getchar with custom port configuration

Source

pub fn emit_putchar(&mut self)

Emit putchar routine (blocking write, char in A)

Labels created: putchar, putchar_wait

Source

pub fn emit_putchar_config(&mut self, config: &MC6850Config)

Emit putchar with custom port configuration

Source

pub fn emit_newline(&mut self)

Emit newline routine (prints CR LF)

Labels created: newline Requires: putchar

Source

pub fn emit_print_string(&mut self)

Emit print_string routine (prints null-terminated string at HL)

Labels created: print_string, print_string_loop Requires: putchar

Source

pub fn emit_io_routines(&mut self)

Emit all standard I/O routines

Includes: getchar, putchar, newline, print_string

Source§

impl CodeGen

Source

pub fn emit_clear_screen(&mut self)

Emit clear_screen routine (ESC[2J ESC[H)

Labels created: clear_screen Requires: putchar

Source

pub fn emit_cursor_home(&mut self)

Emit cursor_home routine (ESC[H)

Labels created: cursor_home Requires: putchar

Source

pub fn emit_clear_screen_and_home(&mut self)

Emit combined clear_screen + cursor_home

Labels created: clear_screen, cursor_home Requires: putchar

Source

pub fn emit_cursor_pos(&mut self)

Emit cursor_pos routine (ESC[row;colH) Input: B = row (1-based), C = col (1-based)

Labels created: cursor_pos Requires: putchar, print_byte_dec

Source

pub fn emit_clear_to_eol(&mut self)

Emit clear_to_eol routine (ESC[K)

Labels created: clear_to_eol Requires: putchar

Source

pub fn emit_clear_to_eos(&mut self)

Emit clear_to_eos routine (ESC[J) - clear from cursor to end of screen

Labels created: clear_to_eos Requires: putchar

Source

pub fn emit_cursor_hide(&mut self)

Emit cursor_hide routine (ESC[?25l)

Labels created: cursor_hide Requires: putchar

Source

pub fn emit_cursor_show(&mut self)

Emit cursor_show routine (ESC[?25h)

Labels created: cursor_show Requires: putchar

Source

pub fn emit_cursor_up(&mut self)

Emit cursor_up routine (ESC[A) - move cursor up 1 line

Labels created: cursor_up Requires: putchar

Source

pub fn emit_cursor_down(&mut self)

Emit cursor_down routine (ESC[B) - move cursor down 1 line

Labels created: cursor_down Requires: putchar

Source

pub fn emit_cursor_right(&mut self)

Emit cursor_right routine (ESC[C) - move cursor right 1 column

Labels created: cursor_right Requires: putchar

Source

pub fn emit_cursor_left(&mut self)

Emit cursor_left routine (ESC[D) - move cursor left 1 column

Labels created: cursor_left Requires: putchar

Source

pub fn emit_reset_attrs(&mut self)

Emit reset_attrs routine (ESC[0m) - reset all text attributes

Labels created: reset_attrs Requires: putchar

Source

pub fn emit_reverse_video(&mut self)

Emit reverse_video routine (ESC[7m)

Labels created: reverse_video Requires: putchar

Source

pub fn emit_terminal_routines(&mut self)

Emit all terminal routines

Includes: clear_screen, cursor_home, cursor_pos, clear_to_eol, cursor_hide, cursor_show Requires: putchar, print_byte_dec

Source§

impl CodeGen

Source

pub fn emit_print_byte_dec(&mut self)

Emit print_byte_dec routine - prints A as decimal Always prints exactly what’s needed, no leading zeros except for zero itself

Labels created: print_byte_dec Requires: putchar

Source

pub fn emit_div16(&mut self)

Emit div16 routine - 16-bit division HL / DE -> HL quotient, DE remainder

Labels created: div16, div16_loop, div16_done

Source

pub fn emit_mul8(&mut self)

Emit mul8 routine - 8-bit multiply A * B -> HL

Labels created: mul8, mul8_loop

Source

pub fn emit_negate_hl(&mut self)

Emit negate_hl routine - negate HL (two’s complement)

Labels created: negate_hl

Source

pub fn emit_math_routines(&mut self)

Emit all math routines

Source§

impl CodeGen

Convenience extension methods for CodeGen

Source

pub fn emit_startup(&mut self, stack_top: u16)

Standard RetroShield startup sequence Sets up stack pointer and disables interrupts

Source

pub fn ld_hl_label(&mut self, label: &str)

Load HL with address of a label (for string pointers, etc.)

Source

pub fn ld_de_label(&mut self, label: &str)

Load DE with address of a label

Source

pub fn ld_bc_label(&mut self, label: &str)

Load BC with address of a label

Source

pub fn string_const(&mut self, label: &str, s: &str)

Emit a labeled string constant

Source

pub fn include_stdlib(&mut self)

Include all standard library routines This is a convenience method that includes:

  • I/O routines (getchar, putchar, newline, print_string)
  • Terminal routines (clear_screen, cursor_pos, etc.)
  • Math routines (print_byte_dec, div16, etc.)

Trait Implementations§

Source§

impl Default for CodeGen

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.