riscvemu/virtualEei.go
2022-12-28 13:59:35 +13:00

48 lines
1.1 KiB
Go

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