Skip to content

MVP of LC3 VM in Rust#6

Open
tomip01 wants to merge 51 commits intomainfrom
dev
Open

MVP of LC3 VM in Rust#6
tomip01 wants to merge 51 commits intomainfrom
dev

Conversation

@tomip01
Copy link
Copy Markdown
Owner

@tomip01 tomip01 commented Jan 2, 2025

  • Adds functionality to load programs and execute them
  • Contains tests for each instruction
  • Contains integration tests for a happy path and some border cases
  • Each instruction is documented by explaining how each of them is interpreted as binary

tomip01 and others added 30 commits December 26, 2024 09:29
* can process add instruction
* modify register access
* add methods for getting and setting register values
* add tests
Feature: add instructions related to registers
* creates file for trap codes and convertion from u16
* add getc instr
* add new trap errors
@gabrielbosio
Copy link
Copy Markdown

Can load programs and execute it
Contains tests
Add comments

Please provide a more detailed description of the PR. It's important to provide guidance to reviewers. For example:

  • Instead of "Can load programs and execute it" it can be "Adds functionality to load programs and execute them"
  • Instead of "Contains tests" it can be "Contains tests for each instruction" or "Contains integration tests for a happy path and some border cases" or both
  • Instead of "Add comments" it can be "Each instruction is documented with an example" or "Each instruction is documented by explaining how each of them is interpreted as binary"

Comment thread src/main.rs
// collect file to execute
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
println!("make run FILEPATH=<path/to/file>");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a UX improvement, I would add something to notify the user how the VM is meant to be used. For example, instead of printing:

make run FILEPATH=<path/to/file>

It can print something like:

Usage:
make run FILEPATH=<path/to/file>

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, a8e3acb

Comment thread src/lc3/vm.rs Outdated
Comment thread src/lc3/vm.rs Outdated
/// Main execution loop
///
/// Read instruction, increments the PC, execute the instruction
/// Repeat this while the machine is in a state of running
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clarify this. I would involve the running field because it's public so the user knows about this field.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!, the members of the VM being public was an error I didn't update.
The comment's clarification and the remove of pub are here, b37e99d

Comment thread src/lc3/vm.rs Outdated
Comment on lines +46 to +48
/// Get value stored in the register requested
///
/// If register is not 0 to 7, an Error is returned
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would include register_index to document how the function argument affects the function logic. For example:

Suggested change
/// Get value stored in the register requested
///
/// If register is not 0 to 7, an Error is returned
/// Get value stored in the register requested in `register_index`
///
/// If `register_index` is outside of the range from 0 to 7, an Error is returned

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, 2cad29e

Comment thread src/lc3/vm.rs Outdated
Comment on lines +56 to +58
/// Set a value in the register requested
///
/// If register is not 0 to 7, an Error is returned
Copy link
Copy Markdown

@gabrielbosio gabrielbosio Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would include register_index and value to document how the function arguments affect the function logic.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, 2cad29e

Comment thread src/lc3/vm.rs Outdated
Comment on lines +68 to +70
/// Update flags based on the content of the register requested
///
/// If register is not 0 to 7, an Error is returned
Copy link
Copy Markdown

@gabrielbosio gabrielbosio Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would include register to document how the function argument affects the function logic. Also, if in the other functions the argument is called register_index why it's called different here?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, ea89cfd

Comment thread src/lc3/vm.rs Outdated
Ok(())
}

/// Based on the opcode from the instruction bits, executes the corresponding procedure
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would include instr to document how the function argument affects the function logic.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, 3b71fca

Comment thread src/lc3/vm.rs Outdated
Opcode::And => self.and(instr),
Opcode::Ldr => self.ldr(instr),
Opcode::Str => self.str(instr),
Opcode::Rti => Err(VMError::InvalidOpcode),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a short comment on why this opcode is invalid

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, 04491ec

Comment thread src/lc3/vm.rs Outdated
Opcode::Ldi => self.ldi(instr),
Opcode::Sti => self.sti(instr),
Opcode::Jmp => self.jmp(instr),
Opcode::Res => Err(VMError::InvalidOpcode),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a short comment on why this opcode is invalid

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, 04491ec

Comment thread src/lc3/vm.rs Outdated
Comment on lines +146 to +147
/// Stores in DR the addition between SR1 and SR2 or SR1 and imm5 sign extended
/// Bit 5 if it is 1, then is read as a imm5
Copy link
Copy Markdown

@gabrielbosio gabrielbosio Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit 5, SR2 and imm5 doesn't seem to be related according to this documentation. Please refactor this so that the relationship is explicit. For example:

Suggested change
/// Stores in DR the addition between SR1 and SR2 or SR1 and imm5 sign extended
/// Bit 5 if it is 1, then is read as a imm5
/// If Bit 5 is 0, stores in DR the addition between SR1 and SR2.
/// If Bit 5 is 1, stores in DR the addition between SR1 and imm5.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, b5d8659

Comment thread src/lc3/vm.rs Outdated
Comment on lines +171 to +172
/// Stores in DR the and bitwise between SR1 and SR2 or SR1 and imm5 sign extended
/// Bit 5 if it is 1, then is read as a imm5
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit 5, SR2 and imm5 doesn't seem to be related according to this documentation. Please refactor this so that the relationship is explicit.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, b5d8659

Comment thread src/lc3/vm.rs Outdated
Comment on lines +210 to +215
/// In bit 11 is to jump if is negative
/// In bit 10 is to jump if is zero
/// In bit 9 is to jump if positive
///
/// Stores in PC the addition of the PC and the sign extended PCoffset9 only if the condition flag
/// meets any of the bits that are 1
Copy link
Copy Markdown

@gabrielbosio gabrielbosio Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rephrase this

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!, done, e87d92c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants