-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootloader.py
executable file
·264 lines (216 loc) · 7.11 KB
/
bootloader.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/python
# minimal shell for the boneless
# the instruction set
# TODO port to allocator IR
from boneless.arch.opcode import Instr
from boneless.arch.opcode import *
# the firmare constructs
from spork.firmware.base import *
# the library code
from spork.lib.uartIO import UART
from spork.lib.console import Console
from spork.lib.action import Action, dumpEsc
from spork.lib.stringer import Stringer
from spork.lib.ansi_codes import AnsiStrings, Term
from spork.firmware.firmware import Firmware
from spork.lib.alloc import Alloc, GAlloc
# command infrastructure
from spork.lib.commands import MetaCommand, Command
# these automatically get added to the firmware
import spork.lib.base_command
# TODO compress the banner, it is phat.
from spork.lib.banner import banner
# date stamp
import datetime
from spork.logger import logger
log = logger(__name__)
"""
An interactive console shell for the Boneless-v3 cpu"
"""
# TODO convert to inline
class Init(Inline):
"Run this code on reset , device init"
# TODO find best way to attach this to the peripherals.
def instr(self):
w = self.w
reg = self.reg
return [
Rem("Set up the devices"),
Rem("enable the led"),
MOVI(w.temp, 1),
STXA(w.temp, reg.statusled.en),
Rem("load the timer"),
MOVI(w.temp, 0xFFFF),
STXA(w.temp, reg.timer.reload_0),
MOVI(w.temp, 0x00FF),
STXA(w.temp, reg.timer.reload_1),
Rem("enable timer and events"),
MOVI(w.temp, 1),
STXA(w.temp, reg.timer.en),
STXA(w.temp, reg.timer.ev.enable),
Rem("reset the crc"),
MOVI(w.temp, 1),
STXA(w.temp, reg.crc.reset),
]
# A subclass of Command will add the name into the command line search list
# TODO limit the auto insert.
class show(Command):
"List the escape code Enumerator names"
class _show(SubR):
def setup(self):
self.locals = ["temp"]
# mark the SubR so it is included without
# being called
# IF you don't mark it , the code will not be included.
self.mark()
def instr(self):
# show the escape stings.
s = dumpEsc()
return [s()]
# Bind the subroutine to the code
call = _show()
class al(Command):
"allocation testing"
class _al_test(SubR):
def setup(self):
self.locals = ["temp"]
# mark the SubR so it is included without
# being called
self.mark()
def instr(self):
w = self.w
al = GAlloc()
u = UART()
ho = u.writeHex
return [
MOVI(w.temp, 8),
al(w.temp),
self.globals.heap_pointer(w.temp),
LD(w.temp, w.temp, 0),
ho(w.temp),
]
# Bind the subroutine to the code
call = _al_test()
class ON(Command):
"switch on the led"
class _ledon(SubR):
def setup(self):
self.locals = ["temp"]
# mark the SubR so it is included without
# being called
self.mark()
def instr(self):
w = self.w
return [MOVI(w.temp, 1), STXA(w.temp, self.reg.statusled.led)]
# Bind the subroutine to the code
call = _ledon()
class OFF(Command):
"switch off the led"
class _ledoff(SubR):
def setup(self):
self.locals = ["temp"]
self.mark()
def instr(self):
w = self.w
return [MOVI(w.temp, 0), STXA(w.temp, self.reg.statusled.led)]
call = _ledoff()
class Bootloader(Firmware):
LOADER_ID = "ZIG_0"
# TODO check requirements
requires = ["timer", "uart", "crc", "led"]
def setup(self):
"registers in the bottom Window"
self.w.req(
["temp", "pad_address", "address", "checksum", "incoming_word", "status"]
)
def prelude(self):
"code before the main loop"
i = Init(self.w)
return i()
# this code is in spork/firmware/base.py
def extra(self):
"add in the commands"
return MetaCommand.code()
# Code objects need to return a list of ASM instructions to do stuff.
def instr(self):
"Locals and the attached subroutine in the main loop"
" my window "
w = self.w
" my registers "
reg = self.reg
" make some label that are local"
ll = LocalLabels()
# create the subroutines
"""
by making the the SubR are available , if they are called
if they are used they will be added to the firmware.
"""
uart = UART()
List = MetaCommand.List()
" global strings, if they are used they are added"
# stringer global
st = self.stringer
st.loader_id = "\r\n" + self.LOADER_ID
st.greetings = "\r\nBoneless-CPU-v3\r\n"
st.warmboot = "Warmboot!"
st.reset = "Reset!"
st.available = "Available Commands:"
st.escape = "<ESC>"
st.banner = banner.encode("utf-8")
st.prompt = self.LOADER_ID + ">"
st.date = str(datetime.datetime.today()) + "\r\n"
st.backspace = "<BS>"
" load the ansi codes that are used "
AnsiStrings(st)
" make a terminal code object "
term = Term()
" some global _fixed_ references "
self.globals.led = 0
self.globals.cursor = 0
self.globals.heap_end = self.sw - 8 * 32 # leave 16 windows free
# TODO make globals typesafe
" build some data and subroutines "
console = Console()
action = Action()
" list of instructions to run "
" main loop "
return [
Rem("Write the prelude strings"),
Rem("Banner"),
self.stringer.banner(w.temp),
uart.writestring(w.temp),
Rem("Time Stamp"),
self.stringer.date(w.temp),
uart.writestring(w.temp),
Rem("Hello"),
self.stringer.greetings(w.temp),
uart.writestring(w.temp),
uart.cr(),
Rem("Write the prompt"),
self.stringer.prompt(w.temp),
uart.writestring(w.temp),
Rem("load the pad address into the register"),
# console.pad(w.pad_address),
Rem("Primary Loop"),
ll("loop"),
Rem("get the uart status"),
uart.read(ret=[w.incoming_word, w.status]),
Rem("if the status is zero skip"),
CMPI(w.status, 0),
BZ(ll.skip),
Rem("process the keystroke"),
console(w.incoming_word, w.pad_address, w.status, ret=[w.status]),
action(w.pad_address, w.status, ret=[w.status]),
ll("skip"),
Rem("Timer event"),
# TODO timer event
J(ll.loop),
]
" load this "
firmware = Bootloader
if __name__ == "__main__":
from spork.upload import Uploader
import fwtest
spork = fwtest.build(Bootloader, detail=False)
up = Uploader()
up.upload(spork)