cpu: use EEI terminology

This commit is contained in:
mappu 2022-12-28 13:59:35 +13:00
parent c46b1e515d
commit 29a94dd442
6 changed files with 54 additions and 50 deletions

View File

@ -20,7 +20,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
sw := riscvemu.MemoryWorld{ sw := riscvemu.VirtualEEI{
Ram: make([]byte, *memorySize), Ram: make([]byte, *memorySize),
} }

4
cpu.go
View File

@ -11,10 +11,10 @@ func (e ErrInvalidOpcode) Error() string { return "invalid opcode" }
type CPUState struct { type CPUState struct {
Registers [32]uint32 Registers [32]uint32
Pc uint32 Pc uint32
w World w EEI
} }
func NewCPU(w World) CPUState { func NewCPU(w EEI) CPUState {
return CPUState{w: w} return CPUState{w: w}
} }

View File

@ -6,7 +6,7 @@ import (
func TestCPU(t *testing.T) { func TestCPU(t *testing.T) {
sw := MemoryWorld{ sw := VirtualEEI{
Ram: make([]byte, 1024), Ram: make([]byte, 1024),
} }
_ = sw.Write32(0, 0b00000000000100000000000001110011) // EBREAK _ = sw.Write32(0, 0b00000000000100000000000001110011) // EBREAK

View File

@ -1,45 +0,0 @@
package riscvemu
type MemoryWorld struct {
Ram []byte
}
func (mw *MemoryWorld) ReadByte(addr uint32) (byte, error) {
if addr >= uint32(len(mw.Ram)) {
panic("out of bounds")
}
return mw.Ram[addr], nil
}
func (mw *MemoryWorld) Read32(addr uint32) (uint32, error) {
// n.b. will panic on overflow
return (uint32(mw.Ram[addr]) << 24) | (uint32(mw.Ram[addr+1]) << 16) | (uint32(mw.Ram[addr+2]) << 8) | uint32(mw.Ram[addr+3]), nil
}
func (mw *MemoryWorld) WriteByte(addr uint32, value byte) error {
if addr >= uint32(len(mw.Ram)) {
panic("out of bounds")
}
mw.Ram[addr] = value
return nil
}
func (mw *MemoryWorld) Write32(addr, value uint32) error {
// n.b. will panic on overflow
mw.Ram[addr] = byte((value >> 24) & 0b11111111)
mw.Ram[addr+1] = byte((value >> 16) & 0b11111111)
mw.Ram[addr+2] = byte((value >> 8) & 0b11111111)
mw.Ram[addr+3] = byte(value & 0b11111111)
return nil
}
func (mw *MemoryWorld) Syscall() {
panic("syscall")
}
func (mw *MemoryWorld) Trap() (bool, error) {
panic("trap")
}

47
virtualEei.go Normal file
View File

@ -0,0 +1,47 @@
package riscvemu
type VirtualEEI struct {
Ram []byte
}
func (ve *VirtualEEI) ReadByte(addr uint32) (byte, error) {
if addr >= uint32(len(ve.Ram)) {
panic("out of bounds")
}
return ve.Ram[addr], nil
}
func (ve *VirtualEEI) Read32(addr uint32) (uint32, error) {
// n.b. will panic on overflow
// RISC-V allows 32-bit load/stores to be implemented as either little-endian
// or big-endian. This is the little-endian version
return (uint32(ve.Ram[addr]) << 24) | (uint32(ve.Ram[addr+1]) << 16) | (uint32(ve.Ram[addr+2]) << 8) | uint32(ve.Ram[addr+3]), nil
}
func (ve *VirtualEEI) WriteByte(addr uint32, value byte) error {
if addr >= uint32(len(ve.Ram)) {
panic("out of bounds")
}
ve.Ram[addr] = value
return nil
}
func (ve *VirtualEEI) Write32(addr, value uint32) error {
// n.b. will panic on overflow
ve.Ram[addr] = byte((value >> 24) & 0b11111111)
ve.Ram[addr+1] = byte((value >> 16) & 0b11111111)
ve.Ram[addr+2] = byte((value >> 8) & 0b11111111)
ve.Ram[addr+3] = byte(value & 0b11111111)
return nil
}
func (ve *VirtualEEI) Syscall() {
panic("syscall")
}
func (ve *VirtualEEI) Trap() (bool, error) {
panic("trap")
}

View File

@ -1,6 +1,8 @@
package riscvemu package riscvemu
type World interface { // EEI (Execution Environment Interface) is the enivronment in which RISC-V
// code is run.
type EEI interface {
ReadByte(addr uint32) (byte, error) ReadByte(addr uint32) (byte, error)
Read32(addr uint32) (uint32, error) Read32(addr uint32) (uint32, error)
WriteByte(addr uint32, value byte) error WriteByte(addr uint32, value byte) error