1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 (* ********************************************************************** *)
16 (* Progetto FreeScale *)
19 (* Cosimo Oliboni, oliboni@cs.unibo.it *)
21 (* Questo materiale fa parte della tesi: *)
22 (* "Formalizzazione Interattiva dei Microcontroller a 8bit FreeScale" *)
24 (* data ultima modifica 15/11/2007 *)
25 (* ********************************************************************** *)
27 include "freescale/aux_bases.ma".
29 (* ********************************************** *)
30 (* MATTONI BASE PER DEFINIRE LE TABELLE DELLE MCU *)
31 (* ********************************************** *)
33 (* enumerazione delle ALU *)
34 inductive mcu_type: Type ≝
40 (* enumerazione delle modalita' di indirizzamento = caricamento degli operandi *)
41 inductive instr_mode: Type ≝
42 (* INHERENT = nessun operando *)
44 (* INHERENT = nessun operando (A implicito) *)
45 | MODE_INHA : instr_mode
46 (* INHERENT = nessun operando (X implicito) *)
47 | MODE_INHX : instr_mode
48 (* INHERENT = nessun operando (H implicito) *)
49 | MODE_INHH : instr_mode
51 (* INHERENT_ADDRESS = nessun operando (HX implicito) *)
52 | MODE_INHX0ADD : instr_mode
53 (* INHERENT_ADDRESS = nessun operando (HX implicito+0x00bb) *)
54 | MODE_INHX1ADD : instr_mode
55 (* INHERENT_ADDRESS = nessun operando (HX implicito+0xwwww) *)
56 | MODE_INHX2ADD : instr_mode
58 (* IMMEDIATE = operando valore immediato byte = 0xbb *)
59 | MODE_IMM1 : instr_mode
60 (* IMMEDIATE_EXT = operando valore immediato byte = 0xbb -> esteso a word *)
61 | MODE_IMM1EXT : instr_mode
62 (* IMMEDIATE = operando valore immediato word = 0xwwww *)
63 | MODE_IMM2 : instr_mode
64 (* DIRECT = operando offset byte = [0x00bb] *)
65 | MODE_DIR1 : instr_mode
66 (* DIRECT = operando offset word = [0xwwww] *)
67 | MODE_DIR2 : instr_mode
68 (* INDEXED = nessun operando (implicito [X] *)
69 | MODE_IX0 : instr_mode
70 (* INDEXED = operando offset relativo byte = [X+0x00bb] *)
71 | MODE_IX1 : instr_mode
72 (* INDEXED = operando offset relativo word = [X+0xwwww] *)
73 | MODE_IX2 : instr_mode
74 (* INDEXED = operando offset relativo byte = [SP+0x00bb] *)
75 | MODE_SP1 : instr_mode
76 (* INDEXED = operando offset relativo word = [SP+0xwwww] *)
77 | MODE_SP2 : instr_mode
79 (* DIRECT → DIRECT = carica da diretto/scrive su diretto *)
80 | MODE_DIR1_to_DIR1 : instr_mode
81 (* IMMEDIATE → DIRECT = carica da immediato/scrive su diretto *)
82 | MODE_IMM1_to_DIR1 : instr_mode
83 (* INDEXED++ → DIRECT = carica da [X]/scrive su diretto/H:X++ *)
84 | MODE_IX0p_to_DIR1 : instr_mode
85 (* DIRECT → INDEXED++ = carica da diretto/scrive su [X]/H:X++ *)
86 | MODE_DIR1_to_IX0p : instr_mode
88 (* INHERENT(A) + IMMEDIATE *)
89 | MODE_INHA_and_IMM1 : instr_mode
90 (* INHERENT(X) + IMMEDIATE *)
91 | MODE_INHX_and_IMM1 : instr_mode
92 (* IMMEDIATE + IMMEDIATE *)
93 | MODE_IMM1_and_IMM1 : instr_mode
94 (* DIRECT + IMMEDIATE *)
95 | MODE_DIR1_and_IMM1 : instr_mode
96 (* INDEXED + IMMEDIATE *)
97 | MODE_IX0_and_IMM1 : instr_mode
98 (* INDEXED++ + IMMEDIATE *)
99 | MODE_IX0p_and_IMM1 : instr_mode
100 (* INDEXED + IMMEDIATE *)
101 | MODE_IX1_and_IMM1 : instr_mode
102 (* INDEXED++ + IMMEDIATE *)
103 | MODE_IX1p_and_IMM1 : instr_mode
104 (* INDEXED + IMMEDIATE *)
105 | MODE_SP1_and_IMM1 : instr_mode
107 (* DIRECT(mTNY) = operando offset byte(maschera scrittura implicita 3 bit) *)
108 (* ex: DIR3 e' carica b, scrivi b con n-simo bit modificato *)
109 | MODE_DIRn : oct → instr_mode
110 (* DIRECT(mTNY) + IMMEDIATE = operando offset byte(maschera lettura implicita 3 bit) *)
111 (* + operando valore immediato byte *)
112 (* ex: DIR2_and_IMM1 e' carica b, carica imm, restituisci n-simo bit di b + imm *)
113 | MODE_DIRn_and_IMM1 : oct → instr_mode
114 (* TINY = nessun operando (diretto implicito 4bit = [0x00000000:0000iiii]) *)
115 | MODE_TNY : exadecim → instr_mode
116 (* SHORT = nessun operando (diretto implicito 5bit = [0x00000000:000iiiii]) *)
117 | MODE_SRT : bitrigesim → instr_mode
120 (* enumerazione delle istruzioni di tutte le ALU *)
121 inductive opcode: Type ≝
122 ADC : opcode (* add with carry *)
123 | ADD : opcode (* add *)
124 | AIS : opcode (* add immediate to SP *)
125 | AIX : opcode (* add immediate to X *)
126 | AND : opcode (* and *)
127 | ASL : opcode (* aritmetic shift left *)
128 | ASR : opcode (* aritmetic shift right *)
129 | BCC : opcode (* branch if C=0 *)
130 | BCLRn : opcode (* clear bit n *)
131 | BCS : opcode (* branch if C=1 *)
132 | BEQ : opcode (* branch if Z=1 *)
133 | BGE : opcode (* branch if N⊙V=0 (great or equal) *)
134 | BGND : opcode (* !!background mode!! *)
135 | BGT : opcode (* branch if Z|N⊙V=0 clear (great) *)
136 | BHCC : opcode (* branch if H=0 *)
137 | BHCS : opcode (* branch if H=1 *)
138 | BHI : opcode (* branch if C|Z=0, (higher) *)
139 | BIH : opcode (* branch if nIRQ=1 *)
140 | BIL : opcode (* branch if nIRQ=0 *)
141 | BIT : opcode (* flag = and (bit test) *)
142 | BLE : opcode (* branch if Z|N⊙V=1 (less or equal) *)
143 | BLS : opcode (* branch if C|Z=1 (lower or same) *)
144 | BLT : opcode (* branch if N⊙1=1 (less) *)
145 | BMC : opcode (* branch if I=0 (interrupt mask clear) *)
146 | BMI : opcode (* branch if N=1 (minus) *)
147 | BMS : opcode (* branch if I=1 (interrupt mask set) *)
148 | BNE : opcode (* branch if Z=0 *)
149 | BPL : opcode (* branch if N=0 (plus) *)
150 | BRA : opcode (* branch always *)
151 | BRCLRn : opcode (* branch if bit n clear *)
152 | BRN : opcode (* branch never (nop) *)
153 | BRSETn : opcode (* branch if bit n set *)
154 | BSETn : opcode (* set bit n *)
155 | BSR : opcode (* branch to subroutine *)
156 | CBEQA : opcode (* compare (A) and BEQ *)
157 | CBEQX : opcode (* compare (X) and BEQ *)
158 | CLC : opcode (* C=0 *)
159 | CLI : opcode (* I=0 *)
160 | CLR : opcode (* operand=0 *)
161 | CMP : opcode (* flag = sub (compare A) *)
162 | COM : opcode (* not (1 complement) *)
163 | CPHX : opcode (* flag = sub (compare H:X) *)
164 | CPX : opcode (* flag = sub (compare X) *)
165 | DAA : opcode (* decimal adjust A *)
166 | DBNZ : opcode (* dec and BNE *)
167 | DEC : opcode (* operand=operand-1 (decrement) *)
168 | DIV : opcode (* div *)
169 | EOR : opcode (* xor *)
170 | INC : opcode (* operand=operand+1 (increment) *)
171 | JMP : opcode (* jmp word [operand] *)
172 | JSR : opcode (* jmp to subroutine *)
173 | LDA : opcode (* load in A *)
174 | LDHX : opcode (* load in H:X *)
175 | LDX : opcode (* load in X *)
176 | LSR : opcode (* logical shift right *)
177 | MOV : opcode (* move *)
178 | MUL : opcode (* mul *)
179 | NEG : opcode (* neg (2 complement) *)
180 | NOP : opcode (* nop *)
181 | NSA : opcode (* nibble swap A (al:ah <- ah:al) *)
182 | ORA : opcode (* or *)
183 | PSHA : opcode (* push A *)
184 | PSHH : opcode (* push H *)
185 | PSHX : opcode (* push X *)
186 | PULA : opcode (* pop A *)
187 | PULH : opcode (* pop H *)
188 | PULX : opcode (* pop X *)
189 | ROL : opcode (* rotate left *)
190 | ROR : opcode (* rotate right *)
191 | RSP : opcode (* reset SP (0x00FF) *)
192 | RTI : opcode (* return from interrupt *)
193 | RTS : opcode (* return from subroutine *)
194 | SBC : opcode (* sub with carry*)
195 | SEC : opcode (* C=1 *)
196 | SEI : opcode (* I=1 *)
197 | SHA : opcode (* swap spc_high,A *)
198 | SLA : opcode (* swap spc_low,A *)
199 | STA : opcode (* store from A *)
200 | STHX : opcode (* store from H:X *)
201 | STOP : opcode (* !!stop mode!! *)
202 | STX : opcode (* store from X *)
203 | SUB : opcode (* sub *)
204 | SWI : opcode (* software interrupt *)
205 | TAP : opcode (* flag=A (transfer A to process status byte *)
206 | TAX : opcode (* X=A (transfer A to X) *)
207 | TPA : opcode (* A=flag (transfer process status byte to A) *)
208 | TST : opcode (* flag = sub (test) *)
209 | TSX : opcode (* X:H=SP (transfer SP to H:X) *)
210 | TXA : opcode (* A=X (transfer X to A) *)
211 | TXS : opcode (* SP=X:H (transfer H:X to SP) *)
212 | WAIT : opcode (* !!wait mode!! *)
215 (* introduzione di un tipo opcode dipendente dall'mcu_type (phantom type) *)
216 inductive any_opcode (m:mcu_type) : Type ≝
217 anyOP : opcode → any_opcode m.
219 definition c_anyOP ≝ anyOP.
223 (* raggruppamento di byte e word in un tipo unico *)
224 inductive byte8_or_word16 : Type ≝
225 Byte: byte8 → byte8_or_word16
226 | Word: word16 → byte8_or_word16.
228 definition c_Byte ≝ Byte.
229 definition c_Word ≝ Word.
234 (* opcode → naturali, per usare eqb *)
235 definition magic_of_opcode ≝
236 λo:opcode.match o with
330 (* confronto fra opcode, legale solo se tipati sulla stessa mcu *)
332 λm:mcu_type.λo:any_opcode m.λo':any_opcode m.match o with
333 [ anyOP p ⇒ match o' with
334 [ anyOP p' ⇒ (eq_b8 (magic_of_opcode p) (magic_of_opcode p')) ] ].
336 (* instr_mode → naturali, per usare eqb *)
337 definition magic_of_instr_mode ≝
338 λi:instr_mode.match i with
340 | MODE_INHA ⇒ 〈x0,x1〉
341 | MODE_INHX ⇒ 〈x0,x2〉
342 | MODE_INHH ⇒ 〈x0,x3〉
344 | MODE_INHX0ADD ⇒ 〈x0,x4〉
345 | MODE_INHX1ADD ⇒ 〈x0,x5〉
346 | MODE_INHX2ADD ⇒ 〈x0,x6〉
348 | MODE_IMM1 ⇒ 〈x0,x7〉
349 | MODE_IMM1EXT ⇒ 〈x0,x8〉
350 | MODE_IMM2 ⇒ 〈x0,x9〉
351 | MODE_DIR1 ⇒ 〈x0,xA〉
352 | MODE_DIR2 ⇒ 〈x0,xB〉
359 | MODE_DIR1_to_DIR1 ⇒ 〈x1,x1〉
360 | MODE_IMM1_to_DIR1 ⇒ 〈x1,x2〉
361 | MODE_IX0p_to_DIR1 ⇒ 〈x1,x3〉
362 | MODE_DIR1_to_IX0p ⇒ 〈x1,x4〉
364 | MODE_INHA_and_IMM1 ⇒ 〈x1,x5〉
365 | MODE_INHX_and_IMM1 ⇒ 〈x1,x6〉
366 | MODE_IMM1_and_IMM1 ⇒ 〈x1,x7〉
367 | MODE_DIR1_and_IMM1 ⇒ 〈x1,x8〉
368 | MODE_IX0_and_IMM1 ⇒ 〈x1,x9〉
369 | MODE_IX0p_and_IMM1 ⇒ 〈x1,xA〉
370 | MODE_IX1_and_IMM1 ⇒ 〈x1,xB〉
371 | MODE_IX1p_and_IMM1 ⇒ 〈x1,xC〉
372 | MODE_SP1_and_IMM1 ⇒ 〈x1,xD〉
374 (* 27-34: bisogna considerare l'operando implicito *)
375 | MODE_DIRn o ⇒ plus_b8nc 〈x1,xE〉 〈x0,(exadecim_of_oct o)〉
376 (* 35-42: bisogna considerare l'operando implicito *)
377 | MODE_DIRn_and_IMM1 o ⇒ plus_b8nc 〈x2,x6〉 〈x0,(exadecim_of_oct o)〉
378 (* 43-58: bisogna considerare l'operando implicito *)
379 | MODE_TNY e ⇒ plus_b8nc 〈x2,xE〉 〈x0,e〉
380 (* 59-100: bisogna considerare gli operandi impliciti *)
381 | MODE_SRT t ⇒ plus_b8nc 〈x3,xE〉 (byte8_of_bitrigesim t)
384 (* confronto fra instr_mode *)
386 λi:instr_mode.λi':instr_mode.(eq_b8 (magic_of_instr_mode i) (magic_of_instr_mode i')).
388 (* ********************************************* *)
389 (* STRUMENTI PER LE DIMOSTRAZIONI DI CORRETTEZZA *)
390 (* ********************************************* *)
392 (* su tutta la lista quante volte compare il byte *)
393 let rec get_byte_count (m:mcu_type) (b:byte8) (c:nat)
394 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
397 | cons hd tl ⇒ match thd4T ???? hd with
398 [ Byte b' ⇒ match eq_b8 b b' with
399 [ true ⇒ get_byte_count m b (S c) tl
400 | false ⇒ get_byte_count m b c tl
402 | Word _ ⇒ get_byte_count m b c tl
406 (* su tutta la lista quante volte compare la word (0x9E+byte) *)
407 let rec get_word_count (m:mcu_type) (b:byte8) (c:nat)
408 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
411 | cons hd tl ⇒ match thd4T ???? hd with
412 [ Byte _ ⇒ get_word_count m b c tl
413 | Word w ⇒ match eq_w16 〈〈x9,xE〉:b〉 w with
414 [ true ⇒ get_word_count m b (S c) tl
415 | false ⇒ get_word_count m b c tl
420 (* su tutta la lista quante volte compare lo pseudocodice *)
421 let rec get_pseudo_count (m:mcu_type) (o:opcode) (c:nat)
422 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
425 | cons hd tl ⇒ match fst4T ???? hd with
426 [ anyOP o' ⇒ match eqop m (anyOP m o) (anyOP m o') with
427 [ true ⇒ get_pseudo_count m o (S c) tl
428 | false ⇒ get_pseudo_count m o c tl
433 (* su tutta la lista quante volte compare la modalita' *)
434 let rec get_mode_count (m:mcu_type) (i:instr_mode) (c:nat)
435 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
438 | cons hd tl ⇒ match eqim (snd4T ???? hd) i with
439 [ true ⇒ get_mode_count m i (S c) tl
440 | false ⇒ get_mode_count m i c tl
444 (* b e' non implementato? *)
445 let rec test_not_impl_byte (b:byte8) (l:list byte8) on l ≝
448 | cons hd tl ⇒ match eq_b8 b hd with
450 | false ⇒ test_not_impl_byte b tl
454 (* o e' non implementato? *)
455 let rec test_not_impl_pseudo (o:opcode) (l:list opcode) on l ≝
458 | cons hd tl ⇒ match eqop HC05 (anyOP HC05 o) (anyOP HC05 hd) with
460 | false ⇒ test_not_impl_pseudo o tl
464 (* i e' non implementato? *)
465 let rec test_not_impl_mode (i:instr_mode) (l:list instr_mode) on l ≝
468 | cons hd tl ⇒ match eqim i hd with
470 | false ⇒ test_not_impl_mode i tl
474 (* su tutta la lista quante volte compare la coppia opcode,instr_mode *)
475 let rec get_OpIm_count (m:mcu_type) (o:any_opcode m) (i:instr_mode) (c:nat)
476 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
480 match (eqop m o (fst4T ???? hd)) ⊗
481 (eqim i (snd4T ???? hd)) with
482 [ true ⇒ get_OpIm_count m o i (S c) tl
483 | false ⇒ get_OpIm_count m o i c tl
487 (* iteratore sugli opcode *)
488 definition forall_opcode ≝ λP.
489 P ADC ⊗ P ADD ⊗ P AIS ⊗ P AIX ⊗ P AND ⊗ P ASL ⊗ P ASR ⊗ P BCC ⊗
490 P BCLRn ⊗ P BCS ⊗ P BEQ ⊗ P BGE ⊗ P BGND ⊗ P BGT ⊗ P BHCC ⊗ P BHCS ⊗
491 P BHI ⊗ P BIH ⊗ P BIL ⊗ P BIT ⊗ P BLE ⊗ P BLS ⊗ P BLT ⊗ P BMC ⊗
492 P BMI ⊗ P BMS ⊗ P BNE ⊗ P BPL ⊗ P BRA ⊗ P BRCLRn ⊗ P BRN ⊗ P BRSETn ⊗
493 P BSETn ⊗ P BSR ⊗ P CBEQA ⊗ P CBEQX ⊗ P CLC ⊗ P CLI ⊗ P CLR ⊗ P CMP ⊗
494 P COM ⊗ P CPHX ⊗ P CPX ⊗ P DAA ⊗ P DBNZ ⊗ P DEC ⊗ P DIV ⊗ P EOR ⊗
495 P INC ⊗ P JMP ⊗ P JSR ⊗ P LDA ⊗ P LDHX ⊗ P LDX ⊗ P LSR ⊗ P MOV ⊗
496 P MUL ⊗ P NEG ⊗ P NOP ⊗ P NSA ⊗ P ORA ⊗ P PSHA ⊗ P PSHH ⊗ P PSHX ⊗
497 P PULA ⊗ P PULH ⊗ P PULX ⊗ P ROL ⊗ P ROR ⊗ P RSP ⊗ P RTI ⊗ P RTS ⊗
498 P SBC ⊗ P SEC ⊗ P SEI ⊗ P SHA ⊗ P SLA ⊗ P STA ⊗ P STHX ⊗ P STOP ⊗
499 P STX ⊗ P SUB ⊗ P SWI ⊗ P TAP ⊗ P TAX ⊗ P TPA ⊗ P TST ⊗ P TSX ⊗
500 P TXA ⊗ P TXS ⊗ P WAIT.
502 (* iteratore sulle modalita' *)
503 definition forall_instr_mode ≝ λP.
524 ⊗ P MODE_DIR1_to_DIR1
525 ⊗ P MODE_IMM1_to_DIR1
526 ⊗ P MODE_IX0p_to_DIR1
527 ⊗ P MODE_DIR1_to_IX0p
529 ⊗ P MODE_INHA_and_IMM1
530 ⊗ P MODE_INHX_and_IMM1
531 ⊗ P MODE_IMM1_and_IMM1
532 ⊗ P MODE_DIR1_and_IMM1
533 ⊗ P MODE_IX0_and_IMM1
534 ⊗ P MODE_IX0p_and_IMM1
535 ⊗ P MODE_IX1_and_IMM1
536 ⊗ P MODE_IX1p_and_IMM1
537 ⊗ P MODE_SP1_and_IMM1
539 ⊗ forall_oct (λo. P (MODE_DIRn o))
540 ⊗ forall_oct (λo. P (MODE_DIRn_and_IMM1 o))
541 ⊗ forall_exadecim (λe. P (MODE_TNY e))
542 ⊗ forall_bitrigesim (λt. P (MODE_SRT t)).