Skip to content

Commit

Permalink
chore: docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdud007 committed Oct 28, 2024
1 parent 706b1ae commit 46f788f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
# SolChip8

[Chip8](https://en.wikipedia.org/wiki/CHIP-8) Emulator written in Solidity.

Got helped alot from [An Introduction to Chip-8 Emulation using the Rust Programming Language](https://aquova.net/chip8/chip8.pdf).

- boolean screen support (64 x 32 pixels)
- 16 registers
- 4096 bytes of RAM

### **Supported CHIP-8 Opcodes**

| Opcode | Mnemonic | Description |
| ------ | ----------------- | ------------------------------------------------------------------- |
| 00E0 | **CLS** | Clear the display |
| 00EE | **RET** | Return from a subroutine |
| 1NNN | **JP NNN** | Jump to address `NNN` |
| 2NNN | **CALL NNN** | Call subroutine at `NNN` |
| 3XNN | **SE Vx, NN** | Skip next instruction if `Vx` equals `NN` |
| 4XNN | **SNE Vx, NN** | Skip next instruction if `Vx` does not equal `NN` |
| 5XY0 | **SE Vx, Vy** | Skip next instruction if `Vx` equals `Vy` |
| 6XNN | **LD Vx, NN** | Load value `NN` into register `Vx` |
| 7XNN | **ADD Vx, NN** | Add value `NN` to register `Vx` |
| 8XY0 | **LD Vx, Vy** | Set `Vx` equal to `Vy` |
| 8XY1 | **OR Vx, Vy** | Set `Vx` to `Vx` OR `Vy` |
| 8XY2 | **AND Vx, Vy** | Set `Vx` to `Vx` AND `Vy` |
| 8XY3 | **XOR Vx, Vy** | Set `Vx` to `Vx` XOR `Vy` |
| 8XY4 | **ADD Vx, Vy** | Add `Vy` to `Vx`, set VF to carry |
| 8XY5 | **SUB Vx, Vy** | Subtract `Vy` from `Vx`, set VF to NOT borrow |
| 8XY6 | **SHR Vx** | Shift `Vx` right by 1, set VF to least significant bit |
| 8XY7 | **SUBN Vx, Vy** | Set `Vx` to `Vy` minus `Vx`, set VF to NOT borrow |
| 8XYE | **SHL Vx** | Shift `Vx` left by 1, set VF to most significant bit |
| 9XY0 | **SNE Vx, Vy** | Skip next instruction if `Vx` does not equal `Vy` |
| ANNN | **LD I, NNN** | Set `I` to address `NNN` |
| BNNN | **JP V0, NNN** | Jump to address `V0 + NNN` |
| CXNN | **RND Vx, NN** | Set `Vx` to random byte AND `NN` |
| DXYN | **DRW Vx, Vy, N** | Draw sprite at (`Vx`, `Vy`) with height `N`, set VF on collision |
| EX9E | **SKP Vx** | Skip next instruction if key `Vx` is pressed |
| EXA1 | **SKNP Vx** | Skip next instruction if key `Vx` is not pressed |
| FX07 | **LD Vx, DT** | Set `Vx` to the value of the delay timer |
| FX0A | **LD Vx, K** | Wait for a key press, store the value in `Vx` |
| FX15 | **LD DT, Vx** | Set the delay timer to `Vx` |
| FX18 | **LD ST, Vx** | Set the sound timer to `Vx` |
| FX1E | **ADD I, Vx** | Add `Vx` to `I` |
| FX29 | **LD F, Vx** | Set `I` to the location of the sprite for digit `Vx` |
| FX33 | **LD B, Vx** | Store BCD representation of `Vx` in memory at `I`, `I+1`, and `I+2` |
| FX55 | **LD [I], Vx** | Store registers `V0` to `Vx` in memory starting at `I` |
| FX65 | **LD Vx, [I]** | Read registers `V0` to `Vx` from memory starting at `I` |
4 changes: 0 additions & 4 deletions src/Emu.sol
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,6 @@ contract Emu {
return emu.pc;
}

function getFontset() public view returns (uint8[80] memory) {
return FONTSET;
}

function getRAMValueAt(uint256 index) public view returns (uint8) {
require(index < RAM_SIZE, "RAM index out of bounds");
return emu.ram[index];
Expand Down
20 changes: 3 additions & 17 deletions test/Emu.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ contract EmuTest is Test {

// pc == START_ADDR
uint16 pc = emulator.getPC();
assertEq(
pc,
0x200,
"Program counter should be reset to START_ADDR (0x200)"
);
assertEq(pc, 0x200, "Program counter should be reset to START_ADDR (0x200)");
}

function testExecuteSetVx() public {
Expand Down Expand Up @@ -106,13 +102,7 @@ contract EmuTest is Test {
assertEq(xPos, 0, "Should be 0");
assertEq(yPos, 0, "Should be 0");

uint8[5] memory pixelData = [
program[12],
program[13],
program[14],
program[15],
program[16]
];
uint8[5] memory pixelData = [program[12], program[13], program[14], program[15], program[16]];

// Iterate over each row of the pixel
for (uint8 row = 0; row < 5; row++) {
Expand All @@ -133,11 +123,7 @@ contract EmuTest is Test {
console.log("Index: %s", index);

// Assert that the display pixel matches the expected value
assertEq(
display[index],
pixelShouldBeSet,
"Pixel should be set"
);
assertEq(display[index], pixelShouldBeSet, "Pixel should be set");
}
}
}
Expand Down

0 comments on commit 46f788f

Please sign in to comment.