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 (* IMMEDIATE = operando valore immediato byte = 0xbb *)
52 | MODE_IMM1 : instr_mode
53 (* IMMEDIATE = operando valore immediato word = 0xwwww *)
54 | MODE_IMM2 : instr_mode
55 (* DIRECT = operando offset byte = [0x00bb] *)
56 | MODE_DIR1 : instr_mode
57 (* DIRECT = operando offset word = [0xwwww] *)
58 | MODE_DIR2 : instr_mode
59 (* INDEXED = nessun operando (implicito [X] *)
60 | MODE_IX0 : instr_mode
61 (* INDEXED = operando offset relativo byte = [X+0x00bb] *)
62 | MODE_IX1 : instr_mode
63 (* INDEXED = operando offset relativo word = [X+0xwwww] *)
64 | MODE_IX2 : instr_mode
65 (* INDEXED = operando offset relativo byte = [SP+0x00bb] *)
66 | MODE_SP1 : instr_mode
67 (* INDEXED = operando offset relativo word = [SP+0xwwww] *)
68 | MODE_SP2 : instr_mode
70 (* DIRECT → DIRECT = carica da diretto/scrive su diretto *)
71 | MODE_DIR1_to_DIR1 : instr_mode
72 (* IMMEDIATE → DIRECT = carica da immediato/scrive su diretto *)
73 | MODE_IMM1_to_DIR1 : instr_mode
74 (* INDEXED++ → DIRECT = carica da [X]/scrive su diretto/H:X++ *)
75 | MODE_IX0p_to_DIR1 : instr_mode
76 (* DIRECT → INDEXED++ = carica da diretto/scrive su [X]/H:X++ *)
77 | MODE_DIR1_to_IX0p : instr_mode
79 (* INHERENT(A) + IMMEDIATE *)
80 | MODE_INHA_and_IMM1 : instr_mode
81 (* INHERENT(X) + IMMEDIATE *)
82 | MODE_INHX_and_IMM1 : instr_mode
83 (* IMMEDIATE + IMMEDIATE *)
84 | MODE_IMM1_and_IMM1 : instr_mode
85 (* DIRECT + IMMEDIATE *)
86 | MODE_DIR1_and_IMM1 : instr_mode
87 (* INDEXED + IMMEDIATE *)
88 | MODE_IX0_and_IMM1 : instr_mode
89 (* INDEXED++ + IMMEDIATE *)
90 | MODE_IX0p_and_IMM1 : instr_mode
91 (* INDEXED + IMMEDIATE *)
92 | MODE_IX1_and_IMM1 : instr_mode
93 (* INDEXED++ + IMMEDIATE *)
94 | MODE_IX1p_and_IMM1 : instr_mode
95 (* INDEXED + IMMEDIATE *)
96 | MODE_SP1_and_IMM1 : instr_mode
98 (* DIRECT(mTNY) = operando offset byte(maschera scrittura implicita 3 bit) *)
99 (* ex: DIR3 e' carica b, scrivi b con n-simo bit modificato *)
100 | MODE_DIRn : oct → instr_mode
101 (* DIRECT(mTNY) + IMMEDIATE = operando offset byte(maschera lettura implicita 3 bit) *)
102 (* + operando valore immediato byte *)
103 (* ex: DIR2_and_IMM1 e' carica b, carica imm, restituisci n-simo bit di b + imm *)
104 | MODE_DIRn_and_IMM1 : oct → instr_mode
105 (* TINY = nessun operando (diretto implicito 4bit = [0x00000000:0000iiii]) *)
106 | MODE_TNY : exadecim → instr_mode
107 (* SHORT = nessun operando (diretto implicito 5bit = [0x00000000:000iiiii]) *)
108 | MODE_SRT : bitrigesim → instr_mode
111 (* enumerazione delle istruzioni di tutte le ALU *)
112 inductive opcode: Type ≝
113 ADC : opcode (* add with carry *)
114 | ADD : opcode (* add *)
115 | AIS : opcode (* add immediate to SP *)
116 | AIX : opcode (* add immediate to X *)
117 | AND : opcode (* and *)
118 | ASL : opcode (* aritmetic shift left *)
119 | ASR : opcode (* aritmetic shift right *)
120 | BCC : opcode (* branch if C=0 *)
121 | BCLRn : opcode (* clear bit n *)
122 | BCS : opcode (* branch if C=1 *)
123 | BEQ : opcode (* branch if Z=1 *)
124 | BGE : opcode (* branch if N⊙V=0 (great or equal) *)
125 | BGND : opcode (* !!background mode!! *)
126 | BGT : opcode (* branch if Z|N⊙V=0 clear (great) *)
127 | BHCC : opcode (* branch if H=0 *)
128 | BHCS : opcode (* branch if H=1 *)
129 | BHI : opcode (* branch if C|Z=0, (higher) *)
130 | BIH : opcode (* branch if nIRQ=1 *)
131 | BIL : opcode (* branch if nIRQ=0 *)
132 | BIT : opcode (* flag = and (bit test) *)
133 | BLE : opcode (* branch if Z|N⊙V=1 (less or equal) *)
134 | BLS : opcode (* branch if C|Z=1 (lower or same) *)
135 | BLT : opcode (* branch if N⊙1=1 (less) *)
136 | BMC : opcode (* branch if I=0 (interrupt mask clear) *)
137 | BMI : opcode (* branch if N=1 (minus) *)
138 | BMS : opcode (* branch if I=1 (interrupt mask set) *)
139 | BNE : opcode (* branch if Z=0 *)
140 | BPL : opcode (* branch if N=0 (plus) *)
141 | BRA : opcode (* branch always *)
142 | BRCLRn : opcode (* branch if bit n clear *)
143 | BRN : opcode (* branch never (nop) *)
144 | BRSETn : opcode (* branch if bit n set *)
145 | BSETn : opcode (* set bit n *)
146 | BSR : opcode (* branch to subroutine *)
147 | CBEQA : opcode (* compare (A) and BEQ *)
148 | CBEQX : opcode (* compare (X) and BEQ *)
149 | CLC : opcode (* C=0 *)
150 | CLI : opcode (* I=0 *)
151 | CLR : opcode (* operand=0 *)
152 | CMP : opcode (* flag = sub (compare A) *)
153 | COM : opcode (* not (1 complement) *)
154 | CPHX : opcode (* flag = sub (compare H:X) *)
155 | CPX : opcode (* flag = sub (compare X) *)
156 | DAA : opcode (* decimal adjust A *)
157 | DBNZ : opcode (* dec and BNE *)
158 | DEC : opcode (* operand=operand-1 (decrement) *)
159 | DIV : opcode (* div *)
160 | EOR : opcode (* xor *)
161 | INC : opcode (* operand=operand+1 (increment) *)
162 | JMP : opcode (* jmp word [operand] *)
163 | JSR : opcode (* jmp to subroutine *)
164 | LDA : opcode (* load in A *)
165 | LDHX : opcode (* load in H:X *)
166 | LDX : opcode (* load in X *)
167 | LSR : opcode (* logical shift right *)
168 | MOV : opcode (* move *)
169 | MUL : opcode (* mul *)
170 | NEG : opcode (* neg (2 complement) *)
171 | NOP : opcode (* nop *)
172 | NSA : opcode (* nibble swap A (al:ah <- ah:al) *)
173 | ORA : opcode (* or *)
174 | PSHA : opcode (* push A *)
175 | PSHH : opcode (* push H *)
176 | PSHX : opcode (* push X *)
177 | PULA : opcode (* pop A *)
178 | PULH : opcode (* pop H *)
179 | PULX : opcode (* pop X *)
180 | ROL : opcode (* rotate left *)
181 | ROR : opcode (* rotate right *)
182 | RSP : opcode (* reset SP (0x00FF) *)
183 | RTI : opcode (* return from interrupt *)
184 | RTS : opcode (* return from subroutine *)
185 | SBC : opcode (* sub with carry*)
186 | SEC : opcode (* C=1 *)
187 | SEI : opcode (* I=1 *)
188 | SHA : opcode (* swap spc_high,A *)
189 | SLA : opcode (* swap spc_low,A *)
190 | STA : opcode (* store from A *)
191 | STHX : opcode (* store from H:X *)
192 | STOP : opcode (* !!stop mode!! *)
193 | STX : opcode (* store from X *)
194 | SUB : opcode (* sub *)
195 | SWI : opcode (* software interrupt *)
196 | TAP : opcode (* flag=A (transfer A to process status byte *)
197 | TAX : opcode (* X=A (transfer A to X) *)
198 | TPA : opcode (* A=flag (transfer process status byte to A) *)
199 | TST : opcode (* flag = sub (test) *)
200 | TSX : opcode (* X:H=SP (transfer SP to H:X) *)
201 | TXA : opcode (* A=X (transfer X to A) *)
202 | TXS : opcode (* SP=X:H (transfer H:X to SP) *)
203 | WAIT : opcode (* !!wait mode!! *)
206 (* introduzione di un tipo opcode dipendente dall'mcu_type (phantom type) *)
207 inductive any_opcode (m:mcu_type) : Type ≝
208 anyOP : opcode → any_opcode m.
210 coercion cic:/matita/freescale/opcode/any_opcode.ind#xpointer(1/1/1).
212 (* raggruppamento di byte e word in un tipo unico *)
213 inductive byte8_or_word16 : Type ≝
214 Byte: byte8 → byte8_or_word16
215 | Word: word16 → byte8_or_word16.
217 coercion cic:/matita/freescale/opcode/byte8_or_word16.ind#xpointer(1/1/1).
218 coercion cic:/matita/freescale/opcode/byte8_or_word16.ind#xpointer(1/1/2).
220 (* opcode → naturali, per usare eqb *)
221 definition magic_of_opcode ≝
222 λo:opcode.match o with
316 (* confronto fra opcode, legale solo se tipati sulla stessa mcu *)
318 λm:mcu_type.λo:any_opcode m.λo':any_opcode m.match o with
319 [ anyOP p ⇒ match o' with
320 [ anyOP p' ⇒ (eq_b8 (magic_of_opcode p) (magic_of_opcode p')) ] ].
322 (* instr_mode → naturali, per usare eqb *)
323 definition magic_of_instr_mode ≝
324 λi:instr_mode.match i with
326 | MODE_INHA ⇒ 〈x0,x1〉
327 | MODE_INHX ⇒ 〈x0,x2〉
328 | MODE_INHH ⇒ 〈x0,x3〉
330 | MODE_IMM1 ⇒ 〈x0,x4〉
331 | MODE_IMM2 ⇒ 〈x0,x5〉
332 | MODE_DIR1 ⇒ 〈x0,x6〉
333 | MODE_DIR2 ⇒ 〈x0,x7〉
340 | MODE_DIR1_to_DIR1 ⇒ 〈x0,xD〉
341 | MODE_IMM1_to_DIR1 ⇒ 〈x0,xE〉
342 | MODE_IX0p_to_DIR1 ⇒ 〈x0,xF〉
343 | MODE_DIR1_to_IX0p ⇒ 〈x1,x0〉
345 | MODE_INHA_and_IMM1 ⇒ 〈x1,x1〉
346 | MODE_INHX_and_IMM1 ⇒ 〈x1,x2〉
347 | MODE_IMM1_and_IMM1 ⇒ 〈x1,x3〉
348 | MODE_DIR1_and_IMM1 ⇒ 〈x1,x4〉
349 | MODE_IX0_and_IMM1 ⇒ 〈x1,x5〉
350 | MODE_IX0p_and_IMM1 ⇒ 〈x1,x6〉
351 | MODE_IX1_and_IMM1 ⇒ 〈x1,x7〉
352 | MODE_IX1p_and_IMM1 ⇒ 〈x1,x8〉
353 | MODE_SP1_and_IMM1 ⇒ 〈x1,x9〉
355 (* 26-33: bisogna considerare l'operando implicito *)
356 | MODE_DIRn o ⇒ plus_b8nc 〈x1,xA〉 〈x0,(exadecim_of_oct o)〉
357 (* 34-41: bisogna considerare l'operando implicito *)
358 | MODE_DIRn_and_IMM1 o ⇒ plus_b8nc 〈x2,x2〉 〈x0,(exadecim_of_oct o)〉
359 (* 42-57: bisogna considerare l'operando implicito *)
360 | MODE_TNY e ⇒ plus_b8nc 〈x2,xA〉 〈x0,e〉
361 (* 58-99: bisogna considerare gli operandi impliciti *)
362 | MODE_SRT t ⇒ plus_b8nc 〈x3,xA〉 (byte8_of_bitrigesim t)
365 (* confronto fra instr_mode *)
367 λi:instr_mode.λi':instr_mode.(eq_b8 (magic_of_instr_mode i) (magic_of_instr_mode i')).
369 (* ********************************************* *)
370 (* STRUMENTI PER LE DIMOSTRAZIONI DI CORRETTEZZA *)
371 (* ********************************************* *)
373 (* su tutta la lista quante volte compare il byte *)
374 definition get_byte_count := \lambda m:mcu_type.
375 let rec get_byte_count (b:byte8) (c:nat)
376 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
379 | cons hd tl ⇒ match thd4T ???? hd with
380 [ Byte b' ⇒ match eq_b8 b b' with
381 [ true ⇒ get_byte_count b (S c) tl
382 | false ⇒ get_byte_count b c tl
384 | Word _ ⇒ get_byte_count b c tl
389 (* su tutta la lista quante volte compare la word (0x9E+byte) *)
390 definition get_word_count := \lambda m:mcu_type.
391 let rec get_word_count (b:byte8) (c:nat)
392 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
395 | cons hd tl ⇒ match thd4T ???? hd with
396 [ Byte _ ⇒ get_word_count b c tl
397 | Word w ⇒ match eq_w16 〈〈x9,xE〉:b〉 w with
398 [ true ⇒ get_word_count b (S c) tl
399 | false ⇒ get_word_count b c tl
405 (* su tutta la lista quante volte compare lo pseudocodice *)
406 definition get_pseudo_count := \lambda m:mcu_type.
407 let rec get_pseudo_count (o:opcode) (c:nat)
408 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
411 | cons hd tl ⇒ match fst4T ???? hd with
412 [ anyOP o' ⇒ match eqop m (anyOP m o) (anyOP m o') with
413 [ true ⇒ get_pseudo_count o (S c) tl
414 | false ⇒ get_pseudo_count o c tl
420 (* su tutta la lista quante volte compare la modalita' *)
421 definition get_mode_count := \lambda m:mcu_type.
422 let rec get_mode_count (i:instr_mode) (c:nat)
423 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
426 | cons hd tl ⇒ match eqim (snd4T ???? hd) i with
427 [ true ⇒ get_mode_count i (S c) tl
428 | false ⇒ get_mode_count i c tl
433 (* b e' non implementato? *)
434 let rec test_not_impl_byte (b:byte8) (l:list byte8) on l ≝
437 | cons hd tl ⇒ match eq_b8 b hd with
439 | false ⇒ test_not_impl_byte b tl
443 (* o e' non implementato? *)
444 let rec test_not_impl_pseudo (o:opcode) (l:list opcode) on l ≝
447 | cons hd tl ⇒ match eqop HC05 (anyOP HC05 o) (anyOP HC05 hd) with
449 | false ⇒ test_not_impl_pseudo o tl
453 (* i e' non implementato? *)
454 let rec test_not_impl_mode (i:instr_mode) (l:list instr_mode) on l ≝
457 | cons hd tl ⇒ match eqim i hd with
459 | false ⇒ test_not_impl_mode i tl
463 (* su tutta la lista quante volte compare la coppia opcode,instr_mode *)
464 definition get_OpIm_count := \lambda m:mcu_type.
465 let rec get_OpIm_count (o:any_opcode m) (i:instr_mode) (c:nat)
466 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
470 match (eqop m o (fst4T ???? hd)) ⊗
471 (eqim i (snd4T ???? hd)) with
472 [ true ⇒ get_OpIm_count o i (S c) tl
473 | false ⇒ get_OpIm_count o i c tl
478 (* iteratore sugli opcode *)
479 definition forall_opcode ≝ λP.
480 P ADC ⊗ P ADD ⊗ P AIS ⊗ P AIX ⊗ P AND ⊗ P ASL ⊗ P ASR ⊗ P BCC ⊗
481 P BCLRn ⊗ P BCS ⊗ P BEQ ⊗ P BGE ⊗ P BGND ⊗ P BGT ⊗ P BHCC ⊗ P BHCS ⊗
482 P BHI ⊗ P BIH ⊗ P BIL ⊗ P BIT ⊗ P BLE ⊗ P BLS ⊗ P BLT ⊗ P BMC ⊗
483 P BMI ⊗ P BMS ⊗ P BNE ⊗ P BPL ⊗ P BRA ⊗ P BRCLRn ⊗ P BRN ⊗ P BRSETn ⊗
484 P BSETn ⊗ P BSR ⊗ P CBEQA ⊗ P CBEQX ⊗ P CLC ⊗ P CLI ⊗ P CLR ⊗ P CMP ⊗
485 P COM ⊗ P CPHX ⊗ P CPX ⊗ P DAA ⊗ P DBNZ ⊗ P DEC ⊗ P DIV ⊗ P EOR ⊗
486 P INC ⊗ P JMP ⊗ P JSR ⊗ P LDA ⊗ P LDHX ⊗ P LDX ⊗ P LSR ⊗ P MOV ⊗
487 P MUL ⊗ P NEG ⊗ P NOP ⊗ P NSA ⊗ P ORA ⊗ P PSHA ⊗ P PSHH ⊗ P PSHX ⊗
488 P PULA ⊗ P PULH ⊗ P PULX ⊗ P ROL ⊗ P ROR ⊗ P RSP ⊗ P RTI ⊗ P RTS ⊗
489 P SBC ⊗ P SEC ⊗ P SEI ⊗ P SHA ⊗ P SLA ⊗ P STA ⊗ P STHX ⊗ P STOP ⊗
490 P STX ⊗ P SUB ⊗ P SWI ⊗ P TAP ⊗ P TAX ⊗ P TPA ⊗ P TST ⊗ P TSX ⊗
491 P TXA ⊗ P TXS ⊗ P WAIT.
493 (* iteratore sulle modalita' *)
494 definition forall_instr_mode ≝ λP.
510 ⊗ P MODE_DIR1_to_DIR1
511 ⊗ P MODE_IMM1_to_DIR1
512 ⊗ P MODE_IX0p_to_DIR1
513 ⊗ P MODE_DIR1_to_IX0p
515 ⊗ P MODE_INHA_and_IMM1
516 ⊗ P MODE_INHX_and_IMM1
517 ⊗ P MODE_IMM1_and_IMM1
518 ⊗ P MODE_DIR1_and_IMM1
519 ⊗ P MODE_IX0_and_IMM1
520 ⊗ P MODE_IX0p_and_IMM1
521 ⊗ P MODE_IX1_and_IMM1
522 ⊗ P MODE_IX1p_and_IMM1
523 ⊗ P MODE_SP1_and_IMM1
525 ⊗ forall_oct (λo. P (MODE_DIRn o))
526 ⊗ forall_oct (λo. P (MODE_DIRn_and_IMM1 o))
527 ⊗ forall_exadecim (λe. P (MODE_TNY e))
528 ⊗ forall_bitrigesim (λt. P (MODE_SRT t)).