avr.d 7.34 KB
Newer Older
Axel Simon's avatar
Axel Simon committed
1
2
3
4
5
6
7
8
9
granularity = 16
# export = decode
# 
# val decode = do
#  update@{rd='',rr='',ck='',cs='',cb='',io=''};
#  /
# end

type imm =
Julian Kranz's avatar
Julian Kranz committed
10
   IMM3 of 3
Julian Kranz's avatar
Julian Kranz committed
11
 | IMM4 of 4
Julian Kranz's avatar
Julian Kranz committed
12
 | IMM6 of 6
Julian Kranz's avatar
Julian Kranz committed
13
 | IMM7 of 7
Julian Kranz's avatar
Julian Kranz committed
14
 | IMM8 of 8
Julian Kranz's avatar
Julian Kranz committed
15
 | IMM22 of 22
Julian Kranz's avatar
Julian Kranz committed
16

Axel Simon's avatar
Axel Simon committed
17
type operand =
Julian Kranz's avatar
Julian Kranz committed
18
19
   REG of register
 | REGHL of {regh:register,regl:register}
Julian Kranz's avatar
Julian Kranz committed
20
 | IOREG of io-register
Julian Kranz's avatar
Julian Kranz committed
21
 | IMM of imm
Julian Kranz's avatar
Julian Kranz committed
22

Julian Kranz's avatar
Julian Kranz committed
23
type binop = {first:operand,second:operand}
Julian Kranz's avatar
Julian Kranz committed
24
type unop = {operand:operand}
Julian Kranz's avatar
Julian Kranz committed
25

mb0's avatar
Foo.    
mb0 committed
26
type instruction =
Julian Kranz's avatar
Julian Kranz committed
27
28
   ADC of binop
 | ADD of binop
Axel Simon's avatar
Axel Simon committed
29
 | ADIW of binop
Julian Kranz's avatar
Julian Kranz committed
30
31
 | AND of binop
 | ANDI of binop
Julian Kranz's avatar
Julian Kranz committed
32
33
 | ASR of unop
 | BCLR of unop
Julian Kranz's avatar
Julian Kranz committed
34
 | BLD of binop
Julian Kranz's avatar
Julian Kranz committed
35
36
 | BRBC of binop
 | BRBS of binop
Axel Simon's avatar
Axel Simon committed
37
 | BREAK
Julian Kranz's avatar
Julian Kranz committed
38
 | BSET of unop
Axel Simon's avatar
Axel Simon committed
39
 | BST of binop
Julian Kranz's avatar
Julian Kranz committed
40
41
 | CALL of unop
 | CBI of binop
Axel Simon's avatar
Axel Simon committed
42
43
44
45
46
47
48
49
 | CLC
 | CLH
 | CLI
 | CLN
 | CLS
 | CLT
 | CLV
 | CLZ
Julian Kranz's avatar
Julian Kranz committed
50
51
52
53
54
55
56
 | COM of unop
 | CP of binop
 | CPC of binop
 | CPI of binop
 | CPSE of binop
 | DEC of unop
 | DES of unop
Axel Simon's avatar
Axel Simon committed
57
58
59
 | EICALL
 | EIJMP
 | ELPM
Julian Kranz's avatar
Julian Kranz committed
60

mb0's avatar
Foo.    
mb0 committed
61
type register =
Julian Kranz's avatar
Julian Kranz committed
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
   R0
 | R1
 | R2
 | R3
 | R4
 | R5
 | R6
 | R7
 | R8
 | R9
 | R10
 | R11
 | R12
 | R13
 | R14
 | R15
 | R16
 | R17
 | R18
 | R19
 | R20
 | R21
 | R22
 | R23
 | R24
 | R25
 | R26
 | R27
 | R28
 | R29
 | R30
 | R31

Axel Simon's avatar
Axel Simon committed
95
type io-register =
Julian Kranz's avatar
Julian Kranz committed
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
   IO0
 | IO1
 | IO2
 | IO3
 | IO4
 | IO5
 | IO6
 | IO7
 | IO8
 | IO9
 | IO10
 | IO11
 | IO12
 | IO13
 | IO14
 | IO15
 | IO16
 | IO17
 | IO18
 | IO19
 | IO20
 | IO21
 | IO22
 | IO23
 | IO24
 | IO25
 | IO26
 | IO27
 | IO28
 | IO29
 | IO30
 | IO31

Julian Kranz's avatar
Julian Kranz committed
129
130
val register-from-bits bits =
 case bits of
Julian Kranz's avatar
Julian Kranz committed
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
    '00000': R0
  | '00001': R1
  | '00010': R2
  | '00011': R3
  | '00100': R4
  | '00101': R5
  | '00110': R6
  | '00111': R7
  | '01000': R8
  | '01001': R9
  | '01010': R10
  | '01011': R11
  | '01100': R12
  | '01101': R13
  | '01110': R14
  | '01111': R15
  | '10000': R16
  | '10001': R17
  | '10010': R18
  | '10011': R19
  | '10100': R20
  | '10101': R21
  | '10110': R22
  | '10111': R23
  | '11000': R24
  | '11001': R25
  | '11010': R26
  | '11011': R27
  | '11100': R28
  | '11101': R29
  | '11110': R30
  | '11111': R31
 end
Julian Kranz's avatar
Julian Kranz committed
164

Axel Simon's avatar
Axel Simon committed
165
166
167
val /X = REGHL {regh=R27,regl=R26}
val /Y = REGHL {regh=R29,regl=R28}
val /Z = REGHL {regh=R31,regl=R30}
Julian Kranz's avatar
Julian Kranz committed
168

Julian Kranz's avatar
Julian Kranz committed
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
val io-register-from-bits bits =
 case bits of
    '00000': IO0
  | '00001': IO1
  | '00010': IO2
  | '00011': IO3
  | '00100': IO4
  | '00101': IO5
  | '00110': IO6
  | '00111': IO7
  | '01000': IO8
  | '01001': IO9
  | '01010': IO10
  | '01011': IO11
  | '01100': IO12
  | '01101': IO13
  | '01110': IO14
  | '01111': IO15
  | '10000': IO16
  | '10001': IO17
  | '10010': IO18
  | '10011': IO19
  | '10100': IO20
  | '10101': IO21
  | '10110': IO22
  | '10111': IO23
  | '11000': IO24
  | '11001': IO25
  | '11010': IO26
  | '11011': IO27
  | '11100': IO28
  | '11101': IO29
  | '11110': IO30
  | '11111': IO31
 end

Julian Kranz's avatar
Julian Kranz committed
205
val d ['bit:1'] = do
Axel Simon's avatar
Axel Simon committed
206
207
 rd <- query $rd;
 update@{rd=rd ^ bit}
Julian Kranz's avatar
Julian Kranz committed
208
209
end

Julian Kranz's avatar
Julian Kranz committed
210
val r ['bit:1'] = do
Axel Simon's avatar
Axel Simon committed
211
212
 rr <- query $rr;
 update@{rr=rr ^ bit}
Julian Kranz's avatar
Julian Kranz committed
213
214
end

Julian Kranz's avatar
Julian Kranz committed
215
val k ['bit:1'] = do
Axel Simon's avatar
Axel Simon committed
216
217
 ck <- query $ck;
 update@{ck=ck ^ bit}
Julian Kranz's avatar
Julian Kranz committed
218
219
end

Julian Kranz's avatar
Julian Kranz committed
220
val s ['bit:1'] = do
Axel Simon's avatar
Axel Simon committed
221
222
 cs <- query $cs;
 update@{cs=cs ^ bit}
Julian Kranz's avatar
Julian Kranz committed
223
224
end

Julian Kranz's avatar
Julian Kranz committed
225
val a ['bit:1'] = do
Axel Simon's avatar
Axel Simon committed
226
227
228
229
230
231
232
 io <- query $io;
 update@{io=io ^ bit}
end

val b ['bit:1'] = do
 cb <- query $cb;
 update@{cb=cb ^ bit}
Julian Kranz's avatar
Julian Kranz committed
233
234
end

Julian Kranz's avatar
Julian Kranz committed
235
val rd5 = do
Julian Kranz's avatar
Julian Kranz committed
236
 rd <- query $rd;
Axel Simon's avatar
Axel Simon committed
237
 update @{rd=''};
Julian Kranz's avatar
Julian Kranz committed
238
 return (REG (register-from-bits rd))
Julian Kranz's avatar
Julian Kranz committed
239
end
Julian Kranz's avatar
Julian Kranz committed
240
241
242

val rd4 = do
 rd <- query $rd;
Axel Simon's avatar
Axel Simon committed
243
 update @{rd=''};
Julian Kranz's avatar
Julian Kranz committed
244
 return (REG (register-from-bits ('1' ^ rd)))
Julian Kranz's avatar
Julian Kranz committed
245
end
Julian Kranz's avatar
Julian Kranz committed
246
 
Julian Kranz's avatar
Julian Kranz committed
247
val rr5 = do
Axel Simon's avatar
Axel Simon committed
248
249
 rr <- query $rr;
 update @{rr=''};
Julian Kranz's avatar
Julian Kranz committed
250
 return (REG (register-from-bits rr))
Julian Kranz's avatar
Julian Kranz committed
251
end
Julian Kranz's avatar
Julian Kranz committed
252
253
 
val rr4 = do
Axel Simon's avatar
Axel Simon committed
254
255
 rr <- query $rr;
 update @{rr=''};
Julian Kranz's avatar
Julian Kranz committed
256
 return (REG (register-from-bits ('1' ^ rr)))
Julian Kranz's avatar
Julian Kranz committed
257
end
Julian Kranz's avatar
Julian Kranz committed
258

Julian Kranz's avatar
Julian Kranz committed
259
260
val ck4 = do
 ck <- query $ck;
Axel Simon's avatar
Axel Simon committed
261
 update @{ck=''};
Julian Kranz's avatar
Julian Kranz committed
262
263
264
 return (IMM (IMM4 ck))
end

Julian Kranz's avatar
Julian Kranz committed
265
val ck6 = do
Julian Kranz's avatar
Julian Kranz committed
266
 ck <- query $ck;
Axel Simon's avatar
Axel Simon committed
267
 update @{ck=''};
Julian Kranz's avatar
Julian Kranz committed
268
 return (IMM (IMM6 ck))
Julian Kranz's avatar
Julian Kranz committed
269
270
end

Julian Kranz's avatar
Julian Kranz committed
271
272
val ck7 = do
 ck <- query $ck;
Axel Simon's avatar
Axel Simon committed
273
 update @{ck=''};
Julian Kranz's avatar
Julian Kranz committed
274
275
276
 return (IMM (IMM7 ck))
end

Julian Kranz's avatar
Julian Kranz committed
277
278
val ck8 = do
 ck <- query $ck;
Axel Simon's avatar
Axel Simon committed
279
 update @{ck=''};
Julian Kranz's avatar
Julian Kranz committed
280
281
282
 return (IMM (IMM8 ck))
end

Julian Kranz's avatar
Julian Kranz committed
283
284
val ck22 = do
 ck <- query $ck;
Axel Simon's avatar
Axel Simon committed
285
 update @{ck=''};
Julian Kranz's avatar
Julian Kranz committed
286
287
288
 return (IMM (IMM22 ck))
end

Julian Kranz's avatar
Julian Kranz committed
289
290
val cs3 = do
 cs <- query $cs;
Axel Simon's avatar
Axel Simon committed
291
 update @{cs=''};
Julian Kranz's avatar
Julian Kranz committed
292
 return (IMM (IMM3 cs))
Julian Kranz's avatar
Julian Kranz committed
293
294
end

Julian Kranz's avatar
Julian Kranz committed
295
296
val cb3 = do
 cb <- query $cb;
Axel Simon's avatar
Axel Simon committed
297
 update @{cb=''};
Julian Kranz's avatar
Julian Kranz committed
298
299
300
 return (IMM (IMM3 cb))
end

Julian Kranz's avatar
Julian Kranz committed
301
302
val io = do
 io <- query $io;
Axel Simon's avatar
Axel Simon committed
303
 update @{io=''};
Julian Kranz's avatar
Julian Kranz committed
304
305
306
 return (IOREG (io-register-from-bits io))
end

Julian Kranz's avatar
Julian Kranz committed
307
308
val rd5h-rd5l = do
 rd <- query $rd;
Axel Simon's avatar
Axel Simon committed
309
310
311
312
 rd-regl <- return (register-from-bits ('11' ^ rd ^ '0'));
 rd-regh <- return (register-from-bits ('11' ^ rd ^ '1'));
 update @{rd=''};
 return (REGHL {regh=rd-regh,regl=rd-regl})
Julian Kranz's avatar
Julian Kranz committed
313
314
end

Julian Kranz's avatar
Julian Kranz committed
315
316
317
val binop cons first second = do
 first <- first;
 second <- second;
Axel Simon's avatar
Axel Simon committed
318
 return (cons {first=first, second=second})
Julian Kranz's avatar
Julian Kranz committed
319
320
end

Julian Kranz's avatar
Julian Kranz committed
321
322
val unop cons operand = do
 operand <- operand;
Axel Simon's avatar
Axel Simon committed
323
 return (cons {operand=operand})
Julian Kranz's avatar
Julian Kranz committed
324
325
end

Julian Kranz's avatar
Julian Kranz committed
326
val nullop cons = do
Axel Simon's avatar
Axel Simon committed
327
 return cons
Julian Kranz's avatar
Julian Kranz committed
328
329
end

Julian Kranz's avatar
Julian Kranz committed
330
331
### ADC
###  - Add with Carry
Axel Simon's avatar
Axel Simon committed
332
val / ['000111 r d d d d d r r r r '] = binop ADC rd5 rr5
Julian Kranz's avatar
Julian Kranz committed
333
334
335

### ADD
###  - Add without Carry
Axel Simon's avatar
Axel Simon committed
336
val / ['000011 r d d d d d r r r r '] = binop ADD rd5 rr5
Julian Kranz's avatar
Julian Kranz committed
337
338
339

### ADIW
###  - Add Immediate to Word
Axel Simon's avatar
Axel Simon committed
340
val / ['10010110 k k d d k k k k '] = binop ADIW rd5h-rd5l ck6
Julian Kranz's avatar
Julian Kranz committed
341
342
343

### AND
###  - Logical AND
Axel Simon's avatar
Axel Simon committed
344
val / ['001000 r d d d d d r r r r '] = binop AND rd5 rr5
Julian Kranz's avatar
Julian Kranz committed
345
346
347

### ANDI
###  - Logical AND with Immediate
Axel Simon's avatar
Axel Simon committed
348
val / ['0111 k k k k d d d d k k k k '] = binop ANDI rd4 ck8
Julian Kranz's avatar
Julian Kranz committed
349
350
351

### ASR
###  - Arithmetic Shift Right
Axel Simon's avatar
Axel Simon committed
352
val / ['1001010 d d d d d 0101'] = unop ASR rd5
Julian Kranz's avatar
Julian Kranz committed
353
354
355

### BCLR
###  - Bit Clear in SREG
Axel Simon's avatar
Axel Simon committed
356
val / ['100101001 s s s 1000'] = unop BCLR cs3
Julian Kranz's avatar
Julian Kranz committed
357
358
359

### BLD
###  - Bit Load from the T Flag in SREG to a Bit in Register
Axel Simon's avatar
Axel Simon committed
360
val / ['1111100 d d d d d 0 b b b '] = binop BLD rd5 cb3
Julian Kranz's avatar
Julian Kranz committed
361
362
363

### BRBC
###  - Branch if Bit in SREG is Cleared
Axel Simon's avatar
Axel Simon committed
364
val / ['111101 k k k k k k k s s s '] = binop BRBC cs3 ck7
Julian Kranz's avatar
Julian Kranz committed
365
366
367

### BRBS
###  - Branch if Bit in SREG is Set
Axel Simon's avatar
Axel Simon committed
368
val / ['111100 k k k k k k k s s s '] = binop BRBS cs3 ck7
Julian Kranz's avatar
Julian Kranz committed
369
370
371
372
373
374
375

### BREAK
###  - Break
val / ['1001010110011000'] = nullop BREAK

### BSET
###  - Bit Set in SREG
Axel Simon's avatar
Axel Simon committed
376
val / ['100101000 s s s 1000'] = unop BSET cs3
Julian Kranz's avatar
Julian Kranz committed
377
378
379

### BST
###  - Bit Store from Bit in Register to T Flag in SREG
Axel Simon's avatar
Axel Simon committed
380
val / ['1111101 d d d d d 0 b b b '] = binop BST rd5 cb3
Julian Kranz's avatar
Julian Kranz committed
381
382
383

### CALL
###  - Long Call to a Subroutine
Axel Simon's avatar
Axel Simon committed
384
val / ['1001010 k k k k k 111 k ' 'k k k k k k k k k k k k k k k k '] = unop CALL ck22
Julian Kranz's avatar
Julian Kranz committed
385
386
387

### CBI
###  - Clear Bit in I/O Register
Axel Simon's avatar
Axel Simon committed
388
val / ['10011000 a a a a a b b b '] = binop CBI io cb3
Julian Kranz's avatar
Julian Kranz committed
389
390
391
392

### CLC
###  - Clear Carry Flag
val / ['1001010010001000'] = nullop CLC
Julian Kranz's avatar
Julian Kranz committed
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

### CLH
###  - Clear Half Carry Flag
val / ['1001010011011000'] = nullop CLH

### CLI
###  - Clear Global Interrupt Flag
val / ['1001010011111000'] = nullop CLI

### CLN
###  - Clear Negative Flag
val / ['1001010010101000'] = nullop CLN

### CLS
###  - Clear Signed Flag
val / ['1001010011001000'] = nullop CLS

### CLT
###  - Clear T Flag
val / ['1001010011101000'] = nullop CLT

### CLV
###  - Clear Overflow Flag
val / ['1001010010111000'] = nullop CLV

### CLZ
###  - Clear Zero Flag
val / ['1001010010011000'] = nullop CLZ

### COM
###  - Ones Complement
Axel Simon's avatar
Axel Simon committed
424
val / ['1001010 d d d d d 0000'] = unop COM rd5
Julian Kranz's avatar
Julian Kranz committed
425
426
427

### CP
###  - Compare
Axel Simon's avatar
Axel Simon committed
428
val / ['000101 r d d d d d r r r r '] = binop CP rd5 rr5
Julian Kranz's avatar
Julian Kranz committed
429
430
431

### CPC
###  - Compare with Carry
Axel Simon's avatar
Axel Simon committed
432
val / ['000001 r d d d d d r r r r '] = binop CPC rd5 rr5
Julian Kranz's avatar
Julian Kranz committed
433
434
435

### CPI
###  - Compare with Immediate
Axel Simon's avatar
Axel Simon committed
436
val / ['0011 k k k k d d d d k k k k '] = binop CPI rd4 ck8
Julian Kranz's avatar
Julian Kranz committed
437
438
439

### CPSE
###  - Compare Skip if Equal
Axel Simon's avatar
Axel Simon committed
440
val / ['000100 r d d d d d r r r r '] = binop CPSE rd5 rr5
Julian Kranz's avatar
Julian Kranz committed
441
442
443

### DEC
###  - Decrement
Axel Simon's avatar
Axel Simon committed
444
val / ['1001010 d d d d d 1010'] = unop DEC rd5
Julian Kranz's avatar
Julian Kranz committed
445
446
447

### DES
###  - Data Encryption Standard
Axel Simon's avatar
Axel Simon committed
448
val / ['10010100 k k k k 1011'] = unop DES ck4
Julian Kranz's avatar
Julian Kranz committed
449
450
451
452
453
454
455
456
457
458
459
460
461

### EICALL
###  - Extended Indirect Call to Subroutine
val / ['1001010100011001'] = nullop EICALL

### EIJMP
###  - Extended Indirect Jump
val / ['1001010000011001'] = nullop EIJMP

### ELPM
###  - Extended Load Program Memory
val / ['1001010111011000'] = nullop ELPM
# ...