-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
139 lines (117 loc) · 4.42 KB
/
upload.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const argv = require('minimist')(process.argv.slice(2))
const romWriter = require('./romWriter.js')
const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline')
const util = require('./util.js')
const resetInstruction = Symbol('resetInstruction')
const script = argv._[0]
if (script === undefined) {
throw new Error(`first argument (script) must be defined`)
}
console.log(script)
require('./' + script)
compile()
console.log(`machineCode.length = ${machineCode.length.toString(16)}`)
//let buffer = [
// // hello2, adjusted to use ram page 0x81 for data storage
// //0x80, 0x81, 0x01, 0x01, 0x13, 0x00, 0x04, 0x00, 0x01, 0x00, 0x20, 0x00, 0x46, 0x0a, 0x01, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x40, 0x06,
// //0x0, 0x80, 0x81, 0x70, 0x48, 0x70, 0x65, 0x70, 0x6c, 0x70, 0x6c, 0x70, 0x6f, 0x70, 0x20, 0x70, 0x77, 0x70, 0x6f, 0x70, 0x72, 0x70, 0x6c, 0x70, 0x64, 0x70, 0x21, 0x1, 0x1, 0x13, 0x0, 0x4, 0x0, 0x1, 0x0, 0x20, 0x0, 0x46, 0x23, 0x1, 0x1, 0x20, 0x0, 0x0, 0x0, 0x0, 0x13, 0x0, 0x40, 0x1f, 0x40, 0x1f,
// 0x0, 0x0, 0x0, 0x0, 0x80, 0x81, 0x70, 0x48, 0x70, 0x65, 0x70, 0x6c, 0x70, 0x6c, 0x70, 0x6f, 0x70, 0x20, 0x70, 0x77, 0x70, 0x6f, 0x70, 0x72, 0x70, 0x6c, 0x70, 0x64, 0x70, 0x21, 0x1, 0x1, 0x13, 0x0, 0x4, 0x0, 0x1, 0x0, 0x20, 0x0, 0x4a, 0x26, 0x1, 0x1, 0x20, 0x0, 0x0, 0x0, 0x0, 0x13, 0x0, 0x40, 0x22, 0x40, 0x22
// //0x80, 0xff, 0x70, 0x48, 0x70, 0x65, 0x70, 0x6c, 0x70, 0x6c, 0x70, 0x6f, 0x70, 0x20, 0x70, 0x77, 0x70, 0x6f, 0x70, 0x72, 0x70, 0x6c, 0x70, 0x64, 0x70, 0x21, 0x1, 0x1, 0x13, 0x0, 0x4, 0x0, 0x1, 0x0, 0x20, 0x0, 0x4a, 0x22, 0x1, 0x1, 0x20, 0x0, 0x0, 0x0, 0x0, 0x13, 0x0, 0x40, 0x1e, 0x40, 0x1e
//]
let port
function upload(machineCode, interactiveModeWhenFinished = false) {
const buffer = [
resetInstruction,
char('p'), // select programming mode
...machineCode,
0xff, // an extra byte to allow completion of the intervening instructions which write to ram for the good bytes before this one
resetInstruction, // reset after keyboard buffer has finished being output to bus (but probably before the bios has a chance to write it to ram)
char('x'), // select execution mode
]
console.log('Waiting for "READY" from arduino!')
port = new SerialPort('COM4', { baudRate: 57600 })
port.on("open", () => {
console.log('serial port open')
//nextStep()
})
const parser = port.pipe(new Readline({ delimiter: '\n' }))
parser.on('data', data => {
let matches
if (data === 'READY\r') {
console.log('Programming...')
nextStep()
}
else if (matches = data.match(/^k: ([0-9A-F]{1,2})\r/)) {
const byte = parseInt(matches[1], 16)
console.log(`<arduino> KEYBOARD READ: ${byte.toString(16)}: ${String.fromCharCode(byte)}`)
nextStep()
}
else if (data.match(/^MASTER RESET /)) {
// noop
}
else {
console.log('<arduino> ' + data)
}
})
function reset() {
console.log("Sending master reset request!")
port.write([ char('R') ], (err) => {
if (err) { console.log('Error on write: ', err.message); process.exit() }
})
}
function nextStep() {
if (buffer.length) {
const nextBufferElement = buffer.shift()
if (nextBufferElement === resetInstruction) {
reset()
nextStep()
}
else {
const nextByte = nextBufferElement
console.log(`Sending keyboard byte: 0x${util.leftPad(nextByte.toString(16), 2)}`)
port.write([char('k'), nextByte], (err) => {
if (err) { console.log('Error on write: ', err.message); process.exit() }
})
}
}
else {
if (interactiveModeWhenFinished) {
startInteractiveMode()
}
else {
console.log('All done!')
process.exit(0)
}
}
}
}
function char(s) {
return s.charCodeAt(0)
}
let interactiveModeStarted = false
function startInteractiveMode() {
if (interactiveModeStarted) { return }
interactiveModeStarted = true
const readline = require('readline')
readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') {
process.exit()
}
else {
if (str) {
console.log(`uploader interactiveMode: ${str} ${JSON.stringify(key)}`)
port.write([char('k'), char(str)], (err) => {
if (err) { console.log('Error on write: ', err.message); process.exit() }
})
}
else {
console.log(`uploader interactiveMode: keypress !str, key is ${JSON.stringify(key)}`)
}
}
})
}
module.exports.startInteractiveMode = startInteractiveMode
upload(machineCode)