riscvemu/virtualEei.go

73 lines
1.6 KiB
Go

package riscvemu
import (
"math"
"os"
)
type VirtualEEI struct {
memPages map[uint32][4096]byte
}
func NewVirtualEEI() VirtualEEI {
return VirtualEEI{
memPages: make(map[uint32][4096]byte),
}
}
func (ve *VirtualEEI) ReadByte(addr uint32) (byte, error) {
page, _ := ve.memPages[addr>>12]
return page[addr&0b111111111111], 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
page, _ := ve.memPages[addr>>12]
inPageAddr := addr & 0b111111111111
return (uint32(page[inPageAddr]) << 24) | (uint32(page[inPageAddr+1]) << 16) | (uint32(page[inPageAddr+2]) << 8) | uint32(page[inPageAddr+3]), nil
}
func (ve *VirtualEEI) WriteByte(addr uint32, value byte) error {
page, ok := ve.memPages[addr>>12]
// Slices are reference-types, this will write-through into ve.memPages
page[addr&0b111111111111] = value
if !ok {
ve.memPages[addr>>12] = page
}
return nil
}
func (ve *VirtualEEI) Write32(addr, value uint32) error {
// n.b. will panic on overflow
page, ok := ve.memPages[addr>>12]
inPageAddr := addr & 0b111111111111
// Slices are reference-types, this will write-through into ve.memPages
page[inPageAddr] = byte((value >> 24) & 0b11111111)
page[inPageAddr+1] = byte((value >> 16) & 0b11111111)
page[inPageAddr+2] = byte((value >> 8) & 0b11111111)
page[inPageAddr+3] = byte(value & 0b11111111)
if !ok {
ve.memPages[addr>>12] = page
}
return nil
}
func (ve *VirtualEEI) Syscall() {
panic("syscall")
}
func (ve *VirtualEEI) Trap() (bool, error) {
panic("trap")
}