Skip to content

Commit

Permalink
feat: run, fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdud007 committed Oct 28, 2024
1 parent 96a54a4 commit c5d1ac7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/Emu.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ contract Emu {
bool[NUM_KEYS] keys;
uint8 dt;
uint8 st;
uint256 program_size;
}

Emulator emu;
Expand Down Expand Up @@ -160,11 +161,7 @@ contract Emu {
execute(op);
}

function getDisplay()
public
view
returns (bool[SCREEN_WIDTH * SCREEN_HEIGHT] memory)
{
function getDisplay() public view returns (bool[SCREEN_WIDTH * SCREEN_HEIGHT] memory) {
return emu.screen;
}

Expand All @@ -180,6 +177,7 @@ contract Emu {
for (uint256 i = start; i < end; i++) {
emu.ram[i] = data[i - start];
}
emu.program_size = data.length;
}

function tickTimers() public {
Expand All @@ -204,6 +202,17 @@ contract Emu {
return op;
}

function run() public {
require(emu.program_size > 0, "Program size is 0");
for (uint256 i = 0; i < emu.program_size; i++) {
require(emu.pc < RAM_SIZE - 1, "Program counter out of bounds");
// Fetch the opcode
uint16 op = fetch();
// Execute the opcode
execute(op);
}
}

function execute(uint16 op) internal {
// 0000 - Nop - NOP
if (op == 0x0000) return;
Expand All @@ -221,9 +230,7 @@ contract Emu {
return;
}
// 00EE - RET
else if (
digit1 == 0x0 && digit2 == 0x0 && digit3 == 0xE && digit4 == 0xE
) {
else if (digit1 == 0x0 && digit2 == 0x0 && digit3 == 0xE && digit4 == 0xE) {
emu.pc = pop();
return;
}
Expand Down Expand Up @@ -364,17 +371,8 @@ contract Emu {
uint8 x = digit2;
uint8 nn = uint8(op & 0x00FF);
// Pseudo-random number generation (not secure)
uint8 rand = uint8(
uint256(
keccak256(
abi.encodePacked(
block.timestamp,
blockhash(block.number - 1),
emu.pc
)
)
) % 256
);
uint8 rand =
uint8(uint256(keccak256(abi.encodePacked(block.timestamp, blockhash(block.number - 1), emu.pc))) % 256);
emu.v_reg[x] = rand & nn;
return;
}
Expand Down
71 changes: 71 additions & 0 deletions test/Emu.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,75 @@ contract EmuTest is Test {
}
}
}

function testRunSimpleProgram() public {
uint8[] memory program = new uint8[](17);
// 00E0 - CLS
program[0] = 0x00;
program[1] = 0xE0;
// 6000 - LD V0, 0x00
program[2] = 0x60;
program[3] = 0x00;
// 6100 - LD V1, 0x00
program[4] = 0x61;
program[5] = 0x00;
// A20C - LD I, 0x20C
program[6] = 0xA2;
program[7] = 0x0C;
// D015 - DRW V0, V1, 5
program[8] = 0xD0;
program[9] = 0x15;
// 120A - JP 0x20A
program[10] = 0x12;
program[11] = 0x0A;

program[12] = 0xF0;
program[13] = 0x90;
program[14] = 0xF0;
program[15] = 0x90;
program[16] = 0x90;

emulator.load(program);
emulator.run();

bool[64 * 32] memory display = emulator.getDisplay();

// Expected pattern:
// Row 0: 1 1 1 1 0 0 0 0 (0xF0)
// Row 1: 1 0 0 1 0 0 0 0 (0x90)
// Row 2: 1 1 1 1 0 0 0 0 (0xF0)
// Row 3: 1 0 0 1 0 0 0 0 (0x90)
// Row 4: 1 0 0 1 0 0 0 0 (0x90)

// Starting coordinates (V0, V1)
uint8 xPos = emulator.getVRegister(0);
uint8 yPos = emulator.getVRegister(1);
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]];

// Iterate over each row of the pixel
for (uint8 row = 0; row < 5; row++) {
uint8 pixelByte = pixelData[row];
// Iterate over each column (bit) in the pixel row
for (uint8 col = 0; col < 8; col++) {
// Extract the bit at position (7 - col)
bool pixelShouldBeSet = (pixelByte & (0x80 >> col)) != 0;

// Calculate the display index
uint8 x = xPos + col;
uint8 y = yPos + row;
// Ensure coordinates wrap around if they exceed the screen dimensions
x = x % 64;
y = y % 32;

uint256 index = uint256(y) * 64 + uint256(x);
console.log("Index: %s", index);

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

0 comments on commit c5d1ac7

Please sign in to comment.