Skip to content

Commit b176ab8

Browse files
committed
Stack stuff
1 parent 27af490 commit b176ab8

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

gbboi-emu/Opcodes/0xC5.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
namespace gbboi_emu.Opcodes
44
{
55
/// <summary>
6-
/// PUSH
7-
///
6+
/// PUSH BC
87
/// </summary>
98
[OneByteOpcode]
109
public class _0xC5 : IOpcode
1110
{
12-
public string Mnemonic { get; set; } = "PUSH";
11+
public string Mnemonic { get; set; } = "PUSH BC";
1312

1413
public ushort Length { get; set; } = 1;
1514

@@ -19,7 +18,7 @@ public class _0xC5 : IOpcode
1918

2019
public void Execute(Instruction instruction, ICpu cpu, IMmu mmu)
2120
{
22-
throw new NotImplementedException(Mnemonic);
21+
cpu.Stack.Push(cpu.Registers.BC.Value, cpu.Registers, mmu);
2322
}
2423
}
2524
}

gbboi-emu/Stack.cs

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
namespace gbboi_emu
1+
using System;
2+
3+
namespace gbboi_emu
24
{
35
public class Stack
46
{
57
public void Call(ushort address, Registers registers, IMmu mmu)
68
{
7-
// Decrement the SP
8-
// TODO: Stop skipping the first 2 bytes of the stack
9-
//registers.SP.Value -= 2;
9+
registers.SP.Value -= 2;
1010

1111
// Add current PC to the stack
1212
mmu.WriteByte(registers.SP.Value, registers.PC.Registers[0].Value);
@@ -16,6 +16,20 @@ public void Call(ushort address, Registers registers, IMmu mmu)
1616
registers.PC.Value = address;
1717
}
1818

19+
public void Push(ushort value, Registers registers, IMmu mmu)
20+
{
21+
registers.SP.Value -= 2;
22+
23+
Push((byte)(value >> 8), registers, mmu);
24+
Push((byte)(value & 0x00FF), registers, mmu);
25+
}
26+
27+
public void Push(byte value, Registers registers, IMmu mmu)
28+
{
29+
mmu.WriteByte(registers.SP.Value, value);
30+
mmu.WriteByte((ushort)(registers.SP.Value + 1), value);
31+
}
32+
1933
public void Return(Registers registers, IMmu mmu)
2034
{
2135
// Decrement stack pointer

0 commit comments

Comments
 (0)