-
Notifications
You must be signed in to change notification settings - Fork 0
/
bus.go
61 lines (48 loc) · 1.05 KB
/
bus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package vm
import (
"fmt"
"sort"
"sync"
)
type Address uint64
type AddressRange [2]Address
func (r AddressRange) IsInRange(addr Address) bool {
lowerLimit := addr >= r[0]
upperLimit := addr <= r[1]
return upperLimit && lowerLimit
}
type Device interface {
Range() AddressRange
Put(addr Address, val byte)
Get(addr Address) byte
Name() string
}
var devicesMutex sync.Mutex
var devices []Device
func GetFromAddressSpace(addr Address) byte {
fmt.Printf("Bus: GET 0x%x", addr)
for _, device := range devices {
if device.Range().IsInRange(addr) {
fmt.Printf(" -> %s\n", device.Name())
return device.Get(addr)
}
}
fmt.Printf(" -> NULL\n")
// NULL-Device
return 0x00
}
func PutInAddressSpace(addr Address, val byte) {
for _, device := range devices {
if device.Range().IsInRange(addr) {
device.Put(addr, val)
}
}
}
func RegisterDevice(device Device) {
devicesMutex.Lock()
devices = append(devices, device)
sort.Slice(devices, func(i, j int) bool {
return devices[i].Range()[0] < devices[j].Range()[0]
})
devicesMutex.Unlock()
}