~jpl8/lorthc3

4acd3116a1958811cd2ed5ea28bf37f31137bd38 — jpl 1 year, 1 month ago be398a0
Working return stack and toin read/write
2 files changed, 61 insertions(+), 16 deletions(-)

M src/assembler.rs
M src/compiler.rs
M src/assembler.rs => src/assembler.rs +1 -1
@@ 305,7 305,7 @@ impl LC3Assembler {
    }

    pub fn pc(&self) -> Word {
        eprintln!("PC: {:#02X}", self.pc);
        // eprintln!("PC: {:#02X}", self.pc);
        self.pc
    }
}

M src/compiler.rs => src/compiler.rs +60 -15
@@ 58,6 58,8 @@ impl ForthCompiler {
            ("latest", Self::LATEST),
            ("state", Self::STATE),
            ("here", Self::HERE),
            ("sp_addr", Self::SP_ADDR),
            ("rp_addr", Self::RP_ADDR),
            ("space", ' ' as Word),
            ("question", '?' as Word),
        ];


@@ 67,6 69,7 @@ impl ForthCompiler {

        for (label, val) in cells {
            self.asm.label(label);
            eprintln!("{} ADDRESS: {:#X?}", label, self.asm.pc());
            self.asm.fill(AsmAddr::Word(val));
        }
        self


@@ 88,11 91,11 @@ impl ForthCompiler {
        let toin_addr = self.asm.reference_label("toin");
        self.asm.load(R0, toin_addr).store(R1, R0, 0);

        self.asm.label("readlp");
        self.get_char();
        // TODO: Remove out_char
        self.out_char();
        self.asm
            .label("readlp")
            .get_char()
            // TODO: Remove out_char
            .out_char()
            .store(R0, R1, 0)
            .add(R1, R1, AddMode::Immediate(1 as Word))
            .not(R0, R0)


@@ 112,6 115,28 @@ impl ForthCompiler {
        self
    }

    fn out_char(&mut self) -> &mut Self {
        self.asm
            .add(Self::RP, Self::RP, AddMode::Immediate(1))
            .store(R7, Self::RP, 0)
            .out_char()
            .load_register(R7, Self::RP, 0)
            .add(Self::RP, Self::RP, AddMode::Immediate(-1i16 as Word));

        self
    }

    fn get_char(&mut self) -> &mut Self {
        self.asm
            .add(Self::RP, Self::RP, AddMode::Immediate(1))
            .store(R7, Self::RP, 0)
            .get_char()
            .load_register(R7, Self::RP, 0)
            .add(Self::RP, Self::RP, AddMode::Immediate(-1i16 as Word));

        self
    }

    fn define_next(&mut self) -> &mut Self {
        self.asm
            .label("next")


@@ 121,16 146,12 @@ impl ForthCompiler {
        self
    }
    fn parse_words(&mut self) -> &mut Self {
        let token = self.asm.reference_label("token");
        self.asm.subroutine(AsmJsrMode::Addr(token));
        self.asm
            .branch_unconditionally(AsmAddr::Word(-1i16 as Word));

        self.asm.label("token");

        let toin = self.asm.reference_label("toin");
        // self.asm.load_indirect(R3, toin);
        self.asm.load(R3, toin);
        eprintln!("LDI ADDRESS: {:#X?}", self.asm.pc());
        self.asm.load_indirect(R3, toin);
        // self.asm.load(R3, toin);

        let space = self.asm.reference_label("space");
        self.asm.load(R1, space).neg(R1, R1);


@@ 261,7 282,7 @@ impl ForthCompiler {
            .load_register(R2, Self::SP, 3)
            .load_register(R3, R0, 1)
            .and(R3, R3, AndMode::Immediate(0xf)) // mask to get the word length and not the flags
            .sub(R3, R3, R2);
            .sub(R3, R3, R3, R2);

        let no_match = self.asm.reference_label("no_match");
        self.asm.branch(


@@ 273,7 294,7 @@ impl ForthCompiler {
            .label("nextc")
            .load_register(Self::IP, R0, 2)
            .load_register(R3, R1, 0)
            .sub(R3, Self::IP, R3);
            .sub(R3, R3, Self::IP, R3);

        let no_match = self.asm.reference_label("no_match");
        self.asm


@@ 340,7 361,30 @@ impl ForthCompiler {
    fn error(&mut self) -> &mut Self {
        self.asm.label("error");
        let question = self.asm.reference_label("question");
        self.asm.load(R0, question).out_char().out_char();
        self.asm.load(R0, question);
        self.asm.out_char().out_char();

        self
    }

    fn start(&mut self) -> &mut Self {
        self.asm.label("start");

        let toin = self.asm.reference_label("toin");
        self.asm.load(R0, toin);

        let tib = self.asm.reference_label("tib");
        self.asm
            .load(R1, tib)
            .store(R1, R0, 0)
            .and(R2, R2, AndMode::Immediate(0))
            .store(R2, R1, 0);

        let sp_addr = self.asm.reference_label("sp_addr");
        self.asm.load(Self::SP, sp_addr);

        let rp_addr = self.asm.reference_label("rp_addr");
        self.asm.load(Self::RP, rp_addr);

        self
    }


@@ 348,6 392,7 @@ impl ForthCompiler {
    pub fn compile(&mut self) -> Result<&mut Self> {
        self.load_intermediate_cells()
            .error()
            .start()
            .interpret_loop()
            .parse_words()
            .read_input()


@@ 358,7 403,7 @@ impl ForthCompiler {
            .assemble()?;

        eprintln!("{:?}", self.asm.instructions);
        eprintln!("{:?}", self.assembled_instructions);
        // eprintln!("{:?}", self.assembled_instructions);

        Ok(self)
    }