~jpl8/lorthc3

a6782b55cb5a7b33b7cbc1594f7c031487018542 — jpl 1 year, 1 month ago 001577b
Finish line input
1 files changed, 34 insertions(+), 8 deletions(-)

M src/compiler.rs
M src/compiler.rs => src/compiler.rs +34 -8
@@ 115,6 115,10 @@ impl ForthCompiler {
        self
    }

    fn pc_offset(&self, label: Word) -> SignedWord {
        return (label as SignedWord) - (self.pc() as SignedWord) - 1;
    }

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


@@ 127,26 131,36 @@ impl ForthCompiler {
            .flat_map(|w| w.to_be_bytes())
    }

    pub fn jmp_subroutine(&mut self, offset: Word) -> &mut Self {
        self.instr(LC3(JSR(JsrMode::Offset(offset))))
    pub fn branch_unconditionally(&mut self, offset: Word) -> &mut Self {
        self.instr(LC3(BR(
            ConditionCode::POS as Word | ConditionCode::NEG as Word | ConditionCode::ZER as Word,
            offset,
        )))
    }

    fn load_intermediate_cells(&mut self) -> &mut Self {
        self.jmp_subroutine(2);
        self.fill(Self::TIB);
        self.branch_unconditionally(2);
        self.tib = Some(self.pc());
        self.fill(Self::TOIN);
        self.fill(Self::TIB);
        eprintln!("TIB: {:#02X}", self.tib.unwrap());
        self.toin = Some(self.pc());
        self.fill(Self::TOIN);

        self
    }

    pub fn read_input(&mut self) -> Result<&mut Self> {
    fn get_tib(&self) -> Result<Word> {
        if let Some(tib) = self.tib {
            self.load(R1, tib).load_addr(R0, ForthCompiler::TOIN);
            eprintln!("TIB: {:#02X}", self.tib.unwrap());
            Ok(tib)
        } else {
            bail!("TIB not allocated!")
        }
    }

    pub fn read_input(&mut self) -> Result<&mut Self> {
        let tib = self.get_tib()?;
        self.load(R1, tib).load_addr(R0, ForthCompiler::TOIN);

        let loop_top = self.pc();
        self.get_char()


@@ 165,8 179,20 @@ impl ForthCompiler {
        Ok(self)
    }

    pub fn print_tib(&mut self) -> Result<&mut Self> {
        let tib = self.get_tib()?;
        self.instr(LC3(LD(R0, self.pc_offset(tib) as Word)));
        self.instr(LC3(TRAP(PUTS)));
        Ok(self)
    }

    pub fn compile(&mut self) -> Result<&mut Self> {
        self.load_intermediate_cells().read_input()?.halt();
        self.load_intermediate_cells()
            .read_input()?
            .print_tib()?
            .halt();

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

        Ok(self)
    }