-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniAsm.scala
573 lines (500 loc) · 15.8 KB
/
MiniAsm.scala
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/**
* Copyright 2023 Frank Zeyda
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package j1.miniasm
import j1.miniasm.Validation._
import j1.utils.Output
import j1.utils.Extensions.RichShort
import scala.collection.mutable.{ListBuffer}
import scala.collection.mutable.{Map, HashMap}
import scala.io.AnsiColor._
import java.io.PrintWriter
class j1Asm(start: Int = 0) extends MemInterface {
require(isValidAddr(start))
// Maximum amount of usable memory (codespace depends on j1Config).
val MEMSIZE_MAX = 65536 // 128 KB (REVIEW)
// Hash table to record labels during compilation.
private val labels: Map[String, Label] = HashMap[String, Label]()
// Memory regions written to during compilation.
private val regions: ListBuffer[Range] = ListBuffer[Range]()
// Internal memory array to compile programs and data to.
private val memory: Array[Short] = new Array[Short](MEMSIZE_MAX)
// Start of the current compilation segment.
private var cStart: Int = start
// Address to compile the next instruction to.
private var cAddr: Int = start
/* ************ */
/* MemInterface */
/* ************ */
val memSize: Int = {
memory.size
}
def readMem(addr: Int): Short = {
require(addr < memSize)
memory(addr)
}
def writeMem(addr: Int, content: Short): Short = {
require(addr < memSize)
val prevContent = readMem(addr)
memory(addr) = content
prevContent
}
/* *********** */
/* Private API */
/* *********** */
// Fetches a label with a given name from the label table.
// Creates a table entry and instance if the label does not exist.
private def fetchLabel(name: String): Label = {
labels.getOrElseUpdate(name, new Label(name)(this))
}
/* ******************* */
/* REWORKED UNTIL HERE */
/* ******************* */
/* Checks if a given address is used by another region. */
private def usedByRegion(addr: Int): Boolean = {
(cStart until cAddr contains addr) ||
regions.exists {
region => (region contains addr)
}
}
/* Converts a region to a pretty string representation. */
private def strOfRegion(region: Range, font: String = BOLD) = {
f"[${font}0x${region.start}%X${RESET} .." +
f" ${font}0x${region.last }%X${RESET}]"
}
/* Extracts the dsp increment from an instruction. */
private def dspIncOf(insn: Int): Int = {
require(isAluInsn(insn))
(insn & 0x3) >> 0 match {
case 0x0 => 0
case 0x1 => 1
case 0x2 => -2
case 0x3 => -1
}
}
/* Extracts the rsp increment from an instruction. */
private def rspIncOf(insn: Int): Int = {
require(isAluInsn(insn))
(insn & 0xC) >> 2 match {
case 0x0 => 0
case 0x1 => 1
case 0x2 => -2
case 0x3 => -1
}
}
/* Augments the last compiled instruction via a function upd. */
private def augment(opname: String, windBack: Boolean)
(upd: Int => Option[Int]): j1Asm = {
if (cAddr > cStart) {
var lastAddr: Int = cAddr - 1
var lastInsn: Int = memory(lastAddr) & 0xFFFF
require(isValidInsn(lastInsn))
/* NOTE: Tweak to support store.{^,^^} and iostore.{^,^^}. */
if (windBack && lastInsn == Basewords.DROP.encode && lastAddr > cStart) {
lastAddr = cAddr - 2 // wind back to skip over DROP
lastInsn = memory(lastAddr) & 0xFFFF
}
if (isAluInsn(lastInsn)) {
upd(lastInsn) match {
case Some(augInsn) => {
require(isValidInsn(augInsn))
memory(lastAddr) = augInsn.toShort
if (!j1Disasm.known(augInsn.toShort)) {
Output.warn(
f"Augmented instruction ${RED}0x${augInsn}%04X${RESET} " +
f"via ${opname} not part of the ISA.")
}
}
case None => {
val mnemonic: String = j1Disasm.decode(lastInsn.toShort)
Output.warn(
f"Failed to augment ${BLUE}${mnemonic}${RESET} with ${opname}.")
}
}
}
else {
Output.warn(f"Cannot augment a non-ALU instruction 0x${lastInsn}04X.")
}
}
else {
Output.warn("No instruction compiled to augment.")
}
this // to allow for chaining of further augmentation
}
/* Automatically closes the current compilation segment via done. */
private def autoclose() = {
if (cAddr > cStart) {
Output.warn("Automatically closing compilation segment " +
f"(${BOLD}done${RESET} inserted).")
done
}
}
/* ********** */
/* Public API */
/* ********** */
// Disassembles all compiled segments including the open one.
def disasm(): Unit = {
regions.foreach {
region => {
Console.println("Disassembling Segment: " + strOfRegion(region))
j1Disasm(region)(this)
}
}
// Also disassemble the currently open segment (in progress).
if (cAddr > cStart) {
Console.println("Disassembling Segment: " +
strOfRegion(cStart until cAddr) + f" (${RED}open${RESET})")
j1Disasm(cStart until cAddr)(this)
Console.println(f"${cAddr}%04X: ...")
}
}
// Deploys all compiled segments via the provided MemInterface.
def deploy(implicit memIf: MemInterface): Unit = {
autoclose()
// TODO: Check that all labels are located before deploymnet.
// Use the provided memory interface to deploy the program.
regions.foreach {
region => {
Output.debug("Deploying Segment: " + strOfRegion(region))
region.foreach {
addr => {
memIf.writeMem(addr, memory(addr))
val codeword = memory(addr)
val mnemonic = j1Disasm.decode(codeword)
Output.debug(f"MEM[${addr}%04X] := 0x${codeword}%04X" +
f" (${BLUE}${mnemonic}${RESET})")
}
}
}
}
}
/* Writes memory content as a hexadecimal file (for $readmemh). */
def writehex(filename: String, region: Range): Boolean = {
require(0 until MEMSIZE_MAX contains region.start)
require(0 until MEMSIZE_MAX contains region.last)
if (region.isEmpty) {
Output.warn("Writing empty memory region to '${filename}'.")
}
new PrintWriter(filename) {
region.foreach {
addr => println(memory(addr).toHexString)
}
close
}.checkError
}
/* Writes memory content as a hexadecimal file (for $readmemh). */
def writehex(filename: String): Boolean = {
autoclose()
val maxUsedAddr = regions.map(_.last).max
writehex(filename, 0 to maxUsedAddr)
}
/* Writes memory content as a binary file (for $readmemb). */
def writebin(filename: String, region: Range): Boolean = {
require(0 until MEMSIZE_MAX contains region.start)
require(0 until MEMSIZE_MAX contains region.last)
if (region.isEmpty) {
Output.warn("Writing empty memory region to '${filename}'.")
}
new PrintWriter(filename) {
region.foreach {
addr => println(memory(addr).toBinString)
}
close
}.checkError
}
/* Writes memory content as a binary file (for $readmemb). */
def writebin(filename: String): Boolean = {
autoclose()
val maxUsedAddr = regions.map(_.last).max
writebin(filename, 0 to maxUsedAddr)
}
/* ************ */
/* Embedded DSL */
/* ************ */
/* REVIEW: Should all E-DSL methods return "this" for uniformity? */
def init = {
labels.clear()
regions.clear()
memory.mapInPlace(_ => 0) // clear memory
cStart = start
cAddr = start
}
def org(addr: Int) = {
require(isValidAddr(addr))
if (usedByRegion(addr)) {
Output.critical(f"${BLUE}org 0x${addr}%04X${RESET}" +
" overlaps with an existing segment.")
}
// Automatically close current compilation region.
if (cAddr > cStart) {
regions += (cStart until cAddr)
}
cStart = addr
cAddr = addr
}
def done = {
if (cAddr > cStart) {
regions += (cStart until cAddr)
}
cStart = cAddr // open a new region after this one
}
def fill(words: Int = 1, value: Int = 0x0000) = {
require(words >= 0)
for (i <- 1 to words) {
if (usedByRegion(cAddr)) {
Output.critical(f"Compilation at ${YELLOW}0x${cAddr}%04X${RESET}" +
" overlaps with an existing segment.")
}
memory(cAddr) = value.toShort
cAddr = cAddr + 1
}
}
def compile(insn: Int) = {
require(isValidInsn(insn))
require(isValidAddr(cAddr))
if (usedByRegion(cAddr)) {
Output.critical(f"Compilation at ${YELLOW}0x${cAddr}%04X${RESET}" +
" overlaps with an existing segment.")
}
memory(cAddr) = insn.toShort
cAddr = cAddr + 1
}
def compile(alu: Alu): j1Asm = {
compile(alu.encode)
this // to allow for chaining with ".^", ".^^" and ".x".
}
def label(name: String) = {
fetchLabel(name).locate(cAddr)
}
def imm(value: Int) = {
require(isValidImm(value))
compile(InsnMask.IMM | value)
}
// NOTE: push() currently only works for the 16 bit architecture.
// TODO: Implement a generic push() that works for any data width.
def push(value: Int) = {
require(-32768 until 65536 contains value)
if ((value & 0x8000) == 0x0) {
assert(isValidImm(value))
imm(value)
}
else {
val inv_value = (~value) & 0xFFFF
assert(isValidImm(inv_value))
imm(inv_value)
invert // compile bitwise negation
}
}
def jmp(target: Int) = {
require(isValidTarget(target))
compile(InsnMask.JMP | target)
}
def jmp(name: String) = {
compile(InsnMask.JMP | 0x0000)
fetchLabel(name).calledFrom(cAddr - 1) // REVIEW
}
def jpz(target: Int) = {
require(isValidTarget(target))
compile(InsnMask.JPZ | target)
}
def jpz(name: String) = {
compile(InsnMask.JPZ | 0x0000)
fetchLabel(name).calledFrom(cAddr - 1) // REVIEW
}
def call(target: Int) = {
require(isValidTarget(target))
compile(InsnMask.CALL | target)
}
def call(name: String) = {
compile(InsnMask.CALL | 0x0000)
fetchLabel(name).calledFrom(cAddr -1) // REVIEW
}
/* ********* */
/* Basewords */
/* ********* */
def noop = compile(Basewords.NOOP)
def plus = compile(Basewords.PLUS)
def and = compile(Basewords.AND)
def or = compile(Basewords.OR)
def xor = compile(Basewords.XOR)
def invert = compile(Basewords.INVERT)
def equal = compile(Basewords.EQUAL)
def less = compile(Basewords.LESS)
def uless = compile(Basewords.ULESS)
def swap = compile(Basewords.SWAP)
def dup = compile(Basewords.DUP)
def drop = compile(Basewords.DROP)
def over = compile(Basewords.OVER)
def nip = compile(Basewords.NIP)
def to_r = compile(Basewords.TO_R)
def from_r = compile(Basewords.FROM_R)
def r_fetch = compile(Basewords.R_FETCH)
def fetch = compile(Basewords.FETCH)
def iofetch = compile(Basewords.IOFETCH)
def rshift = compile(Basewords.RSHIFT)
def lshift = compile(Basewords.LSHIFT)
def depths = compile(Basewords.DEPTHS)
def protect = compile(Basewords.PROTECT)
def halt = compile(Basewords.HALT)
def exit = compile(Basewords.EXIT)
/* NOTE: The DROP really is needed to recover the TOS. */
def store = {
compile(Basewords.STORE)
compile(Basewords.DROP)
}
/* NOTE: The DROP really is needed to recover the TOS. */
def iostore = {
compile(Basewords.IOSTORE)
compile(Basewords.DROP)
}
/* ************ */
/* Elided Words */
/* ************ */
// Augments an instruction to keep one operand on the stack.
def ^ = {
augment("^", true /* wind back skipping DROP */) {
insn => {
val dspIncNew = dspIncOf(insn) + 1
if ((-2 to 1 contains dspIncNew) &&
(insn & 0x0070) != AluFunc.T2N.mask) { // REVIEW
Some((insn & ~0x3) | (dspIncNew & 0x3))
}
else {
None
}
}
}
}
// Augments an instruction to keep two operands on the stack.
def ^^ = {
augment("^^", true /* wind back skipping DROP */) {
insn => {
val dspIncNew = dspIncOf(insn) + 2
if ((-2 to 1 contains dspIncNew) &&
(insn & 0x0070) == AluFunc.None.mask) {
Some((insn & ~0x3) | (dspIncNew & 0x3) | AluFunc.T2N.mask)
}
else {
None
}
}
}
}
// Augments an instruction to perform an implicit exit.
def x = {
augment("x", false /* do not wind back */) {
insn => { // insn must not already EXIT or change the rsp
if ((insn & 0x0080) == 0x0 && rspIncOf(insn) == 0 &&
(insn & 0x0070) != AluFunc.T2R.mask) { // REVIEW
Some((insn | 0xC /* rsp == -1 */) | AluFunc.EXIT.mask)
}
else {
None
}
}
}
}
/* Elided words that are not agumented base words. */
def rdrop = compile(ElidedWords.RDROP)
def tuck_store = compile(ElidedWords.TUCK_STORE)
/* ************* */
/* Miscellaneous */
/* ************* */
// Restart carries out a jump to address 0x0.
def restart = {
jmp(0x0)
}
// Software HALT operation, using a JMP <PC>.
def softhalt = {
jmp(cAddr)
}
}
object j1Disasm {
// Look-up table for Alu instructions from their encodings.
private val aluLookup: Map[Short, Alu] = HashMap[Short, Alu]()
// Create a look-up table for all known Alu instructions.
Isa.ALLWORDS.foreach {
insn => {
aluLookup(insn.encode) = insn
if (insn.permitsExit) {
aluLookup(withExit(insn).encode) = withExit(insn)
}
}
}
/* *********** */
/* Private API */
/* *********** */
// Infers the name of an instruction based on the Alu subclass name.
private def mkAluName(alu: Alu): String = {
if (alu.isInstanceOf[withExit]) {
mkAluName(alu.asInstanceOf[withExit].wrapped) + "[EXIT]"
}
else {
alu.getClass.getSimpleName
.replace("$", "") // removes '$' suffix from object class
.replaceFirst("(KEEP1_)(.*)", "$2^" )
.replaceFirst("(KEEP2_)(.*)", "$2^^")
}
}
/* ********** */
/* Public API */
/* ********** */
/* Checks if an instruction codeword is known by the disassembler. */
def known(codeword: Short): Boolean = {
if (isAluInsn(codeword)) {
aluLookup contains codeword
}
else true
}
/* Decodes an instruction codeword into a String representation. */
def decode(codeword: Short): String = {
if ((codeword & InsnMask.IMM) != 0x0) {
f"IMM ${codeword & 0x7FFF}"
}
else {
(codeword & 0x6000) match {
case InsnMask.JMP =>
f"JMP ${codeword & 0x1FFF}%04X"
case InsnMask.JPZ =>
f"JPZ ${codeword & 0x1FFF}%04X"
case InsnMask.CALL =>
f"CALL ${codeword & 0x1FFF}%04X"
case InsnMask.ALU => {
aluLookup.get(codeword) match {
case Some(alu) => mkAluName(alu)
case None => f"<${RED}0x${codeword}04X${RESET}>"
}
}
}
}
}
// Create disassembly output for a given address range. */
def apply(start: Int, end: Int)(implicit memIf: MemInterface): Unit = {
assert(isValidAddr(start))
assert(isValidAddr(end))
(start to end).foreach {
addr => {
val codeword = memIf.readMem(addr)
Console.println(
f"${addr}%04X: [${YELLOW}0x${codeword}%04X${RESET}] " +
f"${decode(codeword)}")
}
}
}
// Create disassembly output for a given memory region. */
def apply(region: Range)(implicit memIf: MemInterface): Unit = {
apply(region.start, region.last)
}
}