riscvemu/memoryWorld.go

46 lines
1.0 KiB
Go

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")
}