-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemory.js
More file actions
59 lines (54 loc) · 2.1 KB
/
memory.js
File metadata and controls
59 lines (54 loc) · 2.1 KB
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
const debug = require('debug')('memory')
const firmware = require('./firmware')
const baseAddress = 0x80000000
const memory = {}
exports.getMemory = (address, size) => {
debug(`getMemory ${address.toString(16)} ${size}`)
if (memory[address.toString(16)] === undefined) {
if (firmware[address - baseAddress] === undefined) {
throw new Error('Undefined memory access')
}
if (size === 'byte') {
memory[address.toString(16)] = firmware[address - baseAddress]
} else if (size === 'half-word') {
memory[(address).toString(16)] = firmware[address - baseAddress]
memory[(address + 1).toString(16)] = firmware[address + 1 - baseAddress]
} else if (size === 'word') {
memory[(address).toString(16)] = firmware[address - baseAddress]
memory[(address + 1).toString(16)] = firmware[address + 1 - baseAddress]
memory[(address + 2).toString(16)] = firmware[address + 2- baseAddress]
memory[(address + 3).toString(16)] = firmware[address + 3- baseAddress]
}
}
if (size === 'word') {
const byte1 = memory[address.toString(16)]
const byte2 = memory[(address + 1).toString(16)]
const byte3 = memory[(address + 2).toString(16)]
const byte4 = memory[(address + 3).toString(16)]
return ((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1) >>> 0
} else if (size === 'half-word') {
const byte1 = memory[address.toString(16)]
const byte2 = memory[(address + 1).toString(16)]
return (byte2 << 8) | byte1
} else if (size === 'byte') {
return memory[address.toString(16)]
} else {
throw new Error('TODO')
}
}
exports.setMemory = (address, size, value) => {
debug(`setMemory ${address.toString(16)} ${size} ${value.toString(16)}`)
if (size === 'byte') {
memory[address] = value // TODO: respect size
} else if (size === 'half-word') {
memory[address] = value & 0xFF
memory[address + 1] = value >> 8
} else if (size === 'word') {
memory[address] = value & 0xFF
memory[address + 1] = value >> 8
memory[address + 2] = value >> 16
memory[address + 3] = value >> 24
} else {
throw new Error('TODO')
}
}