I'm hacking together a rough ISS of a processor and I wonder if there is a more efficient way to to what I am doing (ideally without resorting to the unsafe library). (Simplified) I'm representing memory by:
type DataMem []uint8
a register by:
type Register uint16
Memory needs to be in byte sized units and the processor works in larger units as above. This means to store the data we do:
func (m *Machine) ExecuteST (ins Instruction) {
// Store to memory
// Data to store specified in one register
// Address to store to specified by another register
target_address := m.RegBank[ins.Dst]
src_data := m.RegBank[ins.Src]
///////// Start of annoying section////////
var target_0 uint8
var target_1 uint8
target_0 = src_data & 0x0f
target_1 = (src_data & 0xf0)>>8
m.data_mem[target_address ] = target_0
m.data_mem[target_address+1] = target_1
/////////// End of annoying section /////////
m.logger.Printf("ST To Address %x, Data %x
",target_address,src_data)
}
And so on for all sorts of different data types and transfers.
The annoying thing for me is I know the processor that the go code will be running on will have a single load instruction that could do all of the above in a single transfer. I'd expect a good c compiler to optimise into that for me, but without breaking out the unsafe library and going straight to the pointers I don't think I can get around this?
I'm open for suggestions including better ways to model the data memory...