concrete icon indicating copy to clipboard operation
concrete copied to clipboard

Example implementation of LC3

Open JulianGCalderon opened this issue 6 months ago • 3 comments
trafficstars

This PRs adds an example implementation of a LC3 virtual machine, based on Write your Own Virtual Machine, by Justin Meiners and Ryan Pendleton.

I added the full implementation to examples/lc3. Additionally, I changed some elements of the standard library.

The implementation contains some FIX notes, that indicate that I had to use a hacky solution to workaround a current limitation of the language.

Some important features that are missing are:

  • Call a method from inside of a method
  • Take references of struct indexes or array elements: &self.field.inner or &self.field[3].
    • This allows for calling method on struct field elements.
  • If-Else chains.
  • Match statement for non enum values.
  • C-like Enums?
  • Initializing static arrays without specifying every value: [0; 3] instead of [0, 0, 0].
  • Bitwise operations: &, <<, >>.

Debugging is also bit difficult right now, as we don't have a good way to format values. The workaround I found is to write raw data to a file, and read it with another tool. As an example, I added a justfile with some commands that allowed me to inspect the raw data.

Implementation

  • [ ] Inst: BR
  • [x] Inst: ADD
  • [ ] Inst: LD
  • [ ] Inst: ST
  • [ ] Inst: JSR
  • [x] Inst: AND
  • [ ] Inst: LDR
  • [ ] Inst: STR
  • [ ] Inst: RTI
  • [ ] Inst: NOT
  • [ ] Inst: LDI
  • [ ] Inst: STI
  • [ ] Inst: JMP
  • [ ] Inst: RES
  • [x] Inst: LEA
  • [ ] Inst: TRA
    • [ ] Trap: GETC
    • [x] Trap: OUT
    • [x] Trap: PUTS
    • [ ] Trap: IN
    • [ ] Trap: PUTSP
    • [x] Trap: HALT

JulianGCalderon avatar Apr 22 '25 22:04 JulianGCalderon