-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleFrontend.clp
More file actions
537 lines (483 loc) · 16.5 KB
/
SimpleFrontend.clp
File metadata and controls
537 lines (483 loc) · 16.5 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
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
;------------------------------------------------------------------------------
; syn
; Copyright (c) 2013-2017, Joshua Scoggins and Contributors
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
; * Redistributions of source code must retain the above copyright
; notice, this list of conditions and the following disclaimer.
; * Redistributions in binary form must reproduce the above copyright
; notice, this list of conditions and the following disclaimer in the
; documentation and/or other materials provided with the distribution.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
; ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;------------------------------------------------------------------------------
; SimpleFrontend.clp - A program to control various other devices
;------------------------------------------------------------------------------
(batch* cortex.clp)
(batch* MainModuleDeclaration.clp)
(batch* ExternalAddressWrapper.clp)
(batch* Device.clp)
(batch* MemoryBlock.clp)
(batch* Paragraph.clp)
(batch* order.clp)
(defgeneric MAIN::register)
(defgeneric MAIN::list-commands)
(defgeneric MAIN::list-registers)
(deffacts MAIN::initialization
(stage (current init)
(rest)))
(defglobal MAIN
?*rng-device* = /tmp/syn/rng
?*alu-device* = /tmp/syn/alu
?*memory-device* = /tmp/syn/memory
?*gpr-device* = /tmp/syn/gpr
?*blu-device* = /tmp/syn/blu
?*cmp-device* = /tmp/syn/cmp)
(deffunction MAIN::setup
(?device)
(system (format nil
"mkdir /tmp/syn"))
(system (format nil
"rm -f %s"
?device))
(set-socket-name ?device)
(setup-connection)
(printout t "Listening on socket " ?device crlf)
(printout t tab "Before exiting run (shutdown-connection)" crlf))
(deffunction MAIN::stop-connection
()
(shutdown-connection))
(deffunction MAIN::irandom
()
(integer (random)))
(deffunction MAIN::irandom64
()
(binary-or (left-shift (irandom)
32)
(irandom)))
(deffunction MAIN::generic-command
(?device $?args)
(if (write-command ?device
(format nil
"%s callback %s"
(implode$ ?args)
(get-socket-name))) then
(explode$ (read-command))))
(deffunction MAIN::gpr->index
(?register)
(string-to-field (sub-string 2
(str-length ?register)
?register)))
(deffunction MAIN::index->gpr
(?index)
(sym-cat r
?index))
(defmethod MAIN::register
((?value SYMBOL))
(gpr->index ?value))
(defmethod MAIN::register
((?value INTEGER))
?value)
(defmethod MAIN::list-commands
((?device LEXEME)
(?router SYMBOL))
(printout ?router
"List of supported commands for: " ?device crlf)
(if (write-command ?device
(format nil
"list-commands callback %s"
(get-socket-name))) then
(progn$ (?a (explode$ (read-command))) do
(printout ?router
tab "- " ?a crlf))
else
(printout ?router
"Unable to get list of supported commands!" crlf)))
(defmethod MAIN::list-commands
((?device LEXEME))
(list-commands ?device
t))
(deffunction MAIN::shutdown-device
(?device)
(generic-command ?device
shutdown))
(deffunction MAIN::memory-command
($?parameters)
(generic-command ?*memory-device*
?parameters))
(deffunction MAIN::read-memory
(?address)
(bind ?x
(memory-command read
?address))
(if (multifieldp ?x) then
(if (> (length$ ?x) 1) then
?x
else
(nth$ 1
?x))
else
?x))
(deffunction MAIN::write-memory
(?address ?value)
(if (multifieldp (bind ?x
(memory-command write
?address
?value))) then
(if (> (length$ ?x) 1) then
?x
else
(nth$ 1 ?x))
else
?x))
(deffunction MAIN::gpr-command
($?args)
(generic-command ?*gpr-device*
?args))
(deffunction MAIN::get-register
(?address)
(nth$ 1 (gpr-command load
(register ?address))))
(deffunction MAIN::set-register
(?address ?value)
(nth$ 1 (gpr-command store
(register ?address)
?value)))
(deffunction MAIN::increment-register
(?address)
(nth$ 1 (gpr-command ++
(register ?address))))
(deffunction MAIN::decrement-register
(?address)
(nth$ 1 (gpr-command --
(register ?address))))
(deffunction MAIN::get-register-count
()
(nth$ 1 (gpr-command size)))
(deffunction MAIN::blu-command
($?command)
(generic-command ?*blu-device*
?command))
(deffunction MAIN::op:binary-and
(?a ?b)
(blu-command and
?a
?b))
(deffunction MAIN::op:binary-or
(?a ?b)
(blu-command or
?a
?b))
(deffunction MAIN::op:binary-xor
(?a ?b)
(blu-command xor
?a
?b))
(deffunction MAIN::op:binary-nor
(?a ?b)
(blu-command nor
?a
?b))
(deffunction MAIN::op:binary-nand
(?a ?b)
(blu-command nand
?a
?b))
(deffunction MAIN::op:binary-not
(?a)
(blu-command not
?a))
(deffunction MAIN::alu-command
($?args)
(generic-command ?*alu-device*
?args))
(deffunction MAIN::op:add
(?a ?b)
(alu-command add
?a
?b))
(deffunction MAIN::op:add3
(?a ?b ?c)
(op:add (op:add ?a
?b)
?c))
(deffunction MAIN::op:sub
(?a ?b)
(alu-command sub
?a
?b))
(deffunction MAIN::op:mul
(?a ?b)
(alu-command mul
?a
?b))
(deffunction MAIN::op:div
(?a ?b)
; TODO: add support for detecting divide by zero
(alu-command div
?a
?b))
(deffunction MAIN::op:rem
(?a ?b)
; TODO: add support for detecting rem by zero
(alu-command rem
?a
?b))
(deffunction MAIN::op:shift-right
(?a ?b)
(alu-command shift-right
?a
?b))
(deffunction MAIN::op:shift-left
(?a ?b)
(alu-command shift-left
?a
?b))
(deffunction MAIN::op:incr
(?a)
(op:add ?a
1))
(deffunction MAIN::op:decr
(?a)
(op:sub ?a
1))
(deffunction MAIN::op:double
(?a)
(op:shift-left ?a
1))
(deffunction MAIN::op:halve
(?a)
(op:shift-right ?a
1))
(deffunction MAIN::op:square
(?a)
(op:mul ?a
?a))
(deffunction MAIN::op:cube
(?a)
(op:mul (op:mul ?a
?a)
?a))
; wrappers around different units
(deffunction MAIN::op:generic
(?operation $?values)
(funcall (sym-cat op:
?operation)
(expand$ ?values)))
(deffunction MAIN::op:register-register
(?operation ?r0 ?r1)
(op:generic ?operation
(get-register ?r0)
(get-register ?r1)))
(deffunction MAIN::op:register-immediate
(?operation ?r0 ?imm)
(op:generic ?operation
(get-register ?r0)
?imm))
(deffunction MAIN::op:immediate-immediate
(?operation ?imm0 ?imm1)
(op:generic ?operation
?imm0
?imm1))
(deffunction MAIN::op:immediate-unary
(?operation ?imm)
(op:generic ?operation
?imm))
(deffunction MAIN::op:register-unary
(?operation ?r0)
(op:generic ?operation
(get-register ?r0)))
(deffunction MAIN::op:mac
(?a ?b ?c)
(op:add (op:mul ?a
?b)
?c))
(deffunction MAIN::op:register3
(?operation ?r0 ?r1 ?r2)
(op:generic ?operation
(get-register ?r0)
(get-register ?r1)
(get-register ?r2)))
(deffunction MAIN::op:register2-imm
(?operation ?r0 ?r1 ?imm)
(op:generic ?operation
(get-register ?r0)
(get-register ?r1)
?imm))
(deffunction MAIN::cmp-command
($?args)
(generic-command ?*cmp-device*
?args))
(deffunction MAIN::op:neq
(?a ?b)
(cmp-command neq
?a
?b))
(deffunction MAIN::op:eq
(?a ?b)
(cmp-command eq
?a
?b))
(deffunction MAIN::op:lt
(?a ?b)
(cmp-command lt
?a
?b))
(deffunction MAIN::op:gt
(?a ?b)
(cmp-command gt
?a
?b))
(deffunction MAIN::op:le
(?a ?b)
(cmp-command le
?a
?b))
(deffunction MAIN::op:ge
(?a ?b)
(cmp-command ge
?a
?b))
;------------------------------------------------------------------------------
; startup rules
;------------------------------------------------------------------------------
(defrule MAIN::setup-connection
(stage (current init))
?f <- (setup connection ?name)
=>
(retract ?f)
(setup (sym-cat /tmp/syn/
?name)))
(defrule MAIN::populate-rng
(stage (current init))
?f <- (setup rng ?path)
=>
(retract ?f)
(bind ?*rng-device*
?path))
(defrule MAIN::populate-memory
(stage (current init))
?f <- (setup memory ?path)
=>
(retract ?f)
(bind ?*memory-device*
?path))
(defrule MAIN::populate-alu
(stage (current init))
?f <- (setup alu ?path)
=>
(retract ?f)
(bind ?*alu-device*
?path))
(defrule MAIN::populate-gpr
(stage (current init))
?f <- (setup gpr ?path)
=>
(retract ?f)
(bind ?*gpr-device*
?path))
(defrule MAIN::populate-blu
(stage (current init))
?f <- (setup blu ?path)
=>
(retract ?f)
(bind ?*blu-device*
?path))
(defrule MAIN::populate-cmp
(stage (current init))
?f <- (setup cmp ?path)
=>
(retract ?f)
(bind ?*cmp-device*
?path))
;------------------------------------------------------------------------------
; Combination of operations
;------------------------------------------------------------------------------
(deffunction MAIN::jump
(?register ?address)
(set-register ?register
?address))
(deffunction MAIN::advance-pc
(?register)
(increment-register ?register))
(deffunction MAIN::push-value
"Push a value onto the stack"
(?register ?value)
(increment-register ?register)
(write-memory (get-register ?register)
?value))
(deffunction MAIN::pop-value
"Pop a value off the stack"
(?register)
(bind ?result
(read-memory (get-register ?register)))
(decrement-register ?register)
?result)
(deffunction MAIN::indirect-load
"Perform an indirect load from memory (use the address stored at the provided address to load from memory)"
(?address)
(read-memory
(read-memory ?address)))
(deffunction MAIN::indirect-store
"Perform in indirect store into memory (use the address stored at the provided address as the address to store to"
(?address ?value)
(write-memory (read-memory ?address)
?value))
(deffunction MAIN::register-load
"Use the contents of the given register to load from main memory"
(?index)
(read-memory (get-register ?index)))
(deffunction MAIN::register-store
"Use the contents of two registers to store into memory"
(?dest ?value)
(write-memory (get-register ?dest)
(get-register ?value)))
(deffunction MAIN::jump-and-link
(?address ?link ?pc)
(set-register ?link
(+ (get-register ?pc)
1))
(jump ?pc
?address))
(deffunction MAIN::jump-to-register
(?link ?pc)
(set-register ?pc
(get-register ?link)))
(deffunction MAIN::clear-register
(?register)
(set-register ?register
0))
;------------------------------------------------------------------------------
; Print Register Contents
;------------------------------------------------------------------------------
(deffunction MAIN::print-register
(?index ?router)
(printout ?router
tab (format nil
"r%d = 0x%x"
?index
(get-register ?index))
crlf))
(defmethod MAIN::list-registers
((?router SYMBOL))
(printout ?router
"Registers: " crlf)
(loop-for-count (?i 0
(- (get-register-count)
1)) do
(print-register ?i
?router)))
(defmethod MAIN::list-registers
()
(list-registers t))