Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exec #16

Open
wants to merge 2 commits into
base: decode
Choose a base branch
from
Open

Exec #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions zepa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down
69 changes: 67 additions & 2 deletions zepa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"
)

// To-do: refact to avoid duplicate code

func TestFetch(t *testing.T) {

machine := NewMachine(2048)
Expand All @@ -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
Expand All @@ -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])
}
}