diff --git a/zepa.go b/zepa.go index 454aa31..3926540 100644 --- a/zepa.go +++ b/zepa.go @@ -63,17 +63,39 @@ type Machine struct { registers map[Register]uint32 } -func (m *Machine) add(inst Instruction) {} +func (m *Machine) mv(inst Instruction) { + m.registers[inst.rd] = uint32(inst.addrConst) +} -func (m *Machine) sub(inst Instruction) {} +func (m *Machine) add(inst Instruction) { + m.registers[inst.rd] = m.registers[inst.rs1] + m.registers[inst.rs2] +} -func (m *Machine) cmp(inst Instruction) {} +func (m *Machine) sub(inst Instruction) { + m.registers[inst.rd] = m.registers[inst.rs1] - m.registers[inst.rs2] +} + +func (m *Machine) cmp(inst Instruction) { + if m.registers[inst.rs1] == m.registers[inst.rs2] { + m.registers[sr] = 0 + } else if m.registers[inst.rs1] > m.registers[inst.rs2] { + m.registers[sr] = 1 + } else { + m.registers[sr] = 2 + } +} -func (m *Machine) jump(inst Instruction) {} +func (m *Machine) jump(inst Instruction) { + m.registers[pc] = uint32(inst.addrConst) +} -func (m *Machine) load(inst Instruction) {} +func (m *Machine) load(inst Instruction) { + m.registers[inst.rs1] = uint32(inst.addrConst) +} -func (m *Machine) store(inst Instruction) {} +func (m *Machine) store(inst Instruction) { + m.memory[inst.addrConst] = byte(m.registers[inst.rs1]) +} func (m *Machine) fetch() { var completeInstruction uint32 = 0 @@ -158,14 +180,15 @@ func (m *Machine) decode() Instruction { return m.decodeITypeInst(instruction) } -func (m *Machine) execute(inst Instruction) {} +func (m *Machine) execute(inst Instruction) { + inst.opcode(m, inst) +} func (m *Machine) boot() { for { m.fetch() decodedInstruction := m.decode() m.execute(decodedInstruction) - break } } diff --git a/zepa_test.go b/zepa_test.go index 39cff06..871da12 100644 --- a/zepa_test.go +++ b/zepa_test.go @@ -4,6 +4,8 @@ import ( "testing" ) +// To-do: refact to avoid duplicate code + func TestFetch(t *testing.T) { machine := NewMachine(2048) @@ -23,9 +25,7 @@ func TestFetch(t *testing.T) { } } -// To-do: refact to avoid code duplication func TestDecode(t *testing.T) { - machine := NewMachine(2048) machine.memory[0] = 0b00110100 machine.memory[1] = 0b01000011 @@ -51,3 +51,68 @@ func TestDecode(t *testing.T) { t.Errorf("Expected funct7 to be 0, but got %d", decodedInstruction.funct7) } } + +func TestMV(t *testing.T) { + machine := NewMachine(2048) + inst := Instruction{opcode: (*Machine).mv, rd: w0, addrConst: 0xFF} + machine.execute(inst) + + if machine.registers[w0] != 0xFF { + t.Errorf("Expected w0 to be 42, got %d", machine.registers[w0]) + } +} + +func TestADD(t *testing.T) { + machine := NewMachine(2048) + machine.registers[w1] = 66 + machine.registers[w2] = 3000 + inst := Instruction{opcode: (*Machine).add, rd: w0, rs1: w1, rs2: w2} + machine.execute(inst) + + if machine.registers[w0] != 3066 { + t.Errorf("Expected w0 to be 3066, got %d", machine.registers[w0]) + } +} + +func TestSUB(t *testing.T) { + machine := NewMachine(2048) + machine.registers[w1] = 30 + machine.registers[w2] = 10 + inst := Instruction{opcode: (*Machine).sub, rd: w0, rs1: w1, rs2: w2} + machine.execute(inst) + + if machine.registers[w0] != 20 { + t.Errorf("Expected w0 to be 20, got %d", machine.registers[w0]) + } +} + +func TestJUMP(t *testing.T) { + machine := NewMachine(2048) + inst := Instruction{opcode: (*Machine).jump, addrConst: 0xA} + machine.execute(inst) + + if machine.registers[pc] != 0xA { + t.Errorf("Expected pc to be 10, got %d", machine.registers[pc]) + } +} + +func TestLOAD(t *testing.T) { + machine := NewMachine(2048) + inst := Instruction{opcode: (*Machine).load, rs1: w1, addrConst: 256} + machine.execute(inst) + + if machine.registers[w1] != 256 { + t.Errorf("Expected w1 to be 256, got %d", machine.registers[w1]) + } +} + +func TestSTORE(t *testing.T) { + machine := NewMachine(2048) + machine.registers[w1] = 65 + inst := Instruction{opcode: (*Machine).store, rs1: w1, addrConst: 100} + machine.execute(inst) + + if machine.memory[100] != 65 { + t.Errorf("Expected memory value to be 65, got %d", machine.memory[100]) + } +}