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 set "baseuri" "cic:/matita/freescale/opcode/".
29 (*include "/media/VIRTUOSO/freescale/aux_bases.ma".*)
30 include "freescale/aux_bases.ma".
32 (* ********************************************** *)
33 (* MATTONI BASE PER DEFINIRE LE TABELLE DELLE MCU *)
34 (* ********************************************** *)
36 (* enumerazione delle ALU *)
37 inductive mcu_type: Type ≝
43 (* enumerazione delle modalita' di indirizzamento = caricamento degli operandi *)
44 inductive instr_mode: Type ≝
45 (* INHERENT = nessun operando *)
47 (* INHERENT = nessun operando (A implicito) *)
48 | MODE_INHA : instr_mode
49 (* INHERENT = nessun operando (X implicito) *)
50 | MODE_INHX : instr_mode
51 (* INHERENT = nessun operando (H implicito) *)
52 | MODE_INHH : instr_mode
54 (* IMMEDIATE = operando valore immediato byte = 0xbb *)
55 | MODE_IMM1 : instr_mode
56 (* IMMEDIATE = operando valore immediato word = 0xwwww *)
57 | MODE_IMM2 : instr_mode
58 (* DIRECT = operando offset byte = [0x00bb] *)
59 | MODE_DIR1 : instr_mode
60 (* DIRECT = operando offset word = [0xwwww] *)
61 | MODE_DIR2 : instr_mode
62 (* INDEXED = nessun operando (implicito [X] *)
63 | MODE_IX0 : instr_mode
64 (* INDEXED = operando offset relativo byte = [X+0x00bb] *)
65 | MODE_IX1 : instr_mode
66 (* INDEXED = operando offset relativo word = [X+0xwwww] *)
67 | MODE_IX2 : instr_mode
68 (* INDEXED = operando offset relativo byte = [SP+0x00bb] *)
69 | MODE_SP1 : instr_mode
70 (* INDEXED = operando offset relativo word = [SP+0xwwww] *)
71 | MODE_SP2 : instr_mode
73 (* DIRECT → DIRECT = carica da diretto/scrive su diretto *)
74 | MODE_DIR1_to_DIR1 : instr_mode
75 (* IMMEDIATE → DIRECT = carica da immediato/scrive su diretto *)
76 | MODE_IMM1_to_DIR1 : instr_mode
77 (* INDEXED++ → DIRECT = carica da [X]/scrive su diretto/H:X++ *)
78 | MODE_IX0p_to_DIR1 : instr_mode
79 (* DIRECT → INDEXED++ = carica da diretto/scrive su [X]/H:X++ *)
80 | MODE_DIR1_to_IX0p : instr_mode
82 (* INHERENT(A) + IMMEDIATE *)
83 | MODE_INHA_and_IMM1 : instr_mode
84 (* INHERENT(X) + IMMEDIATE *)
85 | MODE_INHX_and_IMM1 : instr_mode
86 (* IMMEDIATE + IMMEDIATE *)
87 | MODE_IMM1_and_IMM1 : instr_mode
88 (* DIRECT + IMMEDIATE *)
89 | MODE_DIR1_and_IMM1 : instr_mode
90 (* INDEXED + IMMEDIATE *)
91 | MODE_IX0_and_IMM1 : instr_mode
92 (* INDEXED++ + IMMEDIATE *)
93 | MODE_IX0p_and_IMM1 : instr_mode
94 (* INDEXED + IMMEDIATE *)
95 | MODE_IX1_and_IMM1 : instr_mode
96 (* INDEXED++ + IMMEDIATE *)
97 | MODE_IX1p_and_IMM1 : instr_mode
98 (* INDEXED + IMMEDIATE *)
99 | MODE_SP1_and_IMM1 : instr_mode
101 (* DIRECT(mTNY) = operando offset byte(maschera scrittura implicita 3 bit) *)
102 (* ex: DIR3 e' carica b, scrivi b con n-simo bit modificato *)
103 | MODE_DIRn : oct → instr_mode
104 (* DIRECT(mTNY) + IMMEDIATE = operando offset byte(maschera lettura implicita 3 bit) *)
105 (* + operando valore immediato byte *)
106 (* ex: DIR2_and_IMM1 e' carica b, carica imm, restituisci n-simo bit di b + imm *)
107 | MODE_DIRn_and_IMM1 : oct → instr_mode
108 (* TINY = nessun operando (diretto implicito 4bit = [0x00000000:0000iiii]) *)
109 | MODE_TNY : exadecim → instr_mode
110 (* SHORT = nessun operando (diretto implicito 5bit = [0x00000000:000iiiii]) *)
111 | MODE_SRT : bitrigesim → instr_mode
114 (* enumerazione delle istruzioni di tutte le ALU *)
115 inductive opcode: Type ≝
116 ADC : opcode (* add with carry *)
117 | ADD : opcode (* add *)
118 | AIS : opcode (* add immediate to SP *)
119 | AIX : opcode (* add immediate to X *)
120 | AND : opcode (* and *)
121 | ASL : opcode (* aritmetic shift left *)
122 | ASR : opcode (* aritmetic shift right *)
123 | BCC : opcode (* branch if C=0 *)
124 | BCLRn : opcode (* clear bit n *)
125 | BCS : opcode (* branch if C=1 *)
126 | BEQ : opcode (* branch if Z=1 *)
127 | BGE : opcode (* branch if N⊙V=0 (great or equal) *)
128 | BGND : opcode (* !!background mode!! *)
129 | BGT : opcode (* branch if Z|N⊙V=0 clear (great) *)
130 | BHCC : opcode (* branch if H=0 *)
131 | BHCS : opcode (* branch if H=1 *)
132 | BHI : opcode (* branch if C|Z=0, (higher) *)
133 | BIH : opcode (* branch if nIRQ=1 *)
134 | BIL : opcode (* branch if nIRQ=0 *)
135 | BIT : opcode (* flag = and (bit test) *)
136 | BLE : opcode (* branch if Z|N⊙V=1 (less or equal) *)
137 | BLS : opcode (* branch if C|Z=1 (lower or same) *)
138 | BLT : opcode (* branch if N⊙1=1 (less) *)
139 | BMC : opcode (* branch if I=0 (interrupt mask clear) *)
140 | BMI : opcode (* branch if N=1 (minus) *)
141 | BMS : opcode (* branch if I=1 (interrupt mask set) *)
142 | BNE : opcode (* branch if Z=0 *)
143 | BPL : opcode (* branch if N=0 (plus) *)
144 | BRA : opcode (* branch always *)
145 | BRCLRn : opcode (* branch if bit n clear *)
146 | BRN : opcode (* branch never (nop) *)
147 | BRSETn : opcode (* branch if bit n set *)
148 | BSETn : opcode (* set bit n *)
149 | BSR : opcode (* branch to subroutine *)
150 | CBEQA : opcode (* compare (A) and BEQ *)
151 | CBEQX : opcode (* compare (X) and BEQ *)
152 | CLC : opcode (* C=0 *)
153 | CLI : opcode (* I=0 *)
154 | CLR : opcode (* operand=0 *)
155 | CMP : opcode (* flag = sub (compare A) *)
156 | COM : opcode (* not (1 complement) *)
157 | CPHX : opcode (* flag = sub (compare H:X) *)
158 | CPX : opcode (* flag = sub (compare X) *)
159 | DAA : opcode (* decimal adjust A *)
160 | DBNZ : opcode (* dec and BNE *)
161 | DEC : opcode (* operand=operand-1 (decrement) *)
162 | DIV : opcode (* div *)
163 | EOR : opcode (* xor *)
164 | INC : opcode (* operand=operand+1 (increment) *)
165 | JMP : opcode (* jmp word [operand] *)
166 | JSR : opcode (* jmp to subroutine *)
167 | LDA : opcode (* load in A *)
168 | LDHX : opcode (* load in H:X *)
169 | LDX : opcode (* load in X *)
170 | LSR : opcode (* logical shift right *)
171 | MOV : opcode (* move *)
172 | MUL : opcode (* mul *)
173 | NEG : opcode (* neg (2 complement) *)
174 | NOP : opcode (* nop *)
175 | NSA : opcode (* nibble swap A (al:ah <- ah:al) *)
176 | ORA : opcode (* or *)
177 | PSHA : opcode (* push A *)
178 | PSHH : opcode (* push H *)
179 | PSHX : opcode (* push X *)
180 | PULA : opcode (* pop A *)
181 | PULH : opcode (* pop H *)
182 | PULX : opcode (* pop X *)
183 | ROL : opcode (* rotate left *)
184 | ROR : opcode (* rotate right *)
185 | RSP : opcode (* reset SP (0x00FF) *)
186 | RTI : opcode (* return from interrupt *)
187 | RTS : opcode (* return from subroutine *)
188 | SBC : opcode (* sub with carry*)
189 | SEC : opcode (* C=1 *)
190 | SEI : opcode (* I=1 *)
191 | SHA : opcode (* swap spc_high,A *)
192 | SLA : opcode (* swap spc_low,A *)
193 | STA : opcode (* store from A *)
194 | STHX : opcode (* store from H:X *)
195 | STOP : opcode (* !!stop mode!! *)
196 | STX : opcode (* store from X *)
197 | SUB : opcode (* sub *)
198 | SWI : opcode (* software interrupt *)
199 | TAP : opcode (* flag=A (transfer A to process status byte *)
200 | TAX : opcode (* X=A (transfer A to X) *)
201 | TPA : opcode (* A=flag (transfer process status byte to A) *)
202 | TST : opcode (* flag = sub (test) *)
203 | TSX : opcode (* X:H=SP (transfer SP to H:X) *)
204 | TXA : opcode (* A=X (transfer X to A) *)
205 | TXS : opcode (* SP=X:H (transfer H:X to SP) *)
206 | WAIT : opcode (* !!wait mode!! *)
209 (* introduzione di un tipo opcode dipendente dall'mcu_type (phantom type) *)
210 inductive any_opcode (m:mcu_type) : Type ≝
211 anyOP : opcode → any_opcode m.
213 coercion cic:/matita/freescale/opcode/any_opcode.ind#xpointer(1/1/1).
215 (* raggruppamento di byte e word in un tipo unico *)
216 inductive byte8_or_word16 : Type ≝
217 Byte: byte8 → byte8_or_word16
218 | Word: word16 → byte8_or_word16.
220 coercion cic:/matita/freescale/opcode/byte8_or_word16.ind#xpointer(1/1/1).
221 coercion cic:/matita/freescale/opcode/byte8_or_word16.ind#xpointer(1/1/2).
223 (* opcode → naturali, per usare eqb *)
224 definition magic_of_opcode ≝
225 λo:opcode.match o with
319 (* confronto fra opcode, legale solo se tipati sulla stessa mcu *)
321 λm:mcu_type.λo:any_opcode m.λo':any_opcode m.match o with
322 [ anyOP p ⇒ match o' with
323 [ anyOP p' ⇒ (eq_b8 (magic_of_opcode p) (magic_of_opcode p')) ] ].
325 (* instr_mode → naturali, per usare eqb *)
326 definition magic_of_instr_mode ≝
327 λi:instr_mode.match i with
329 | MODE_INHA ⇒ 〈x0,x1〉
330 | MODE_INHX ⇒ 〈x0,x2〉
331 | MODE_INHH ⇒ 〈x0,x3〉
333 | MODE_IMM1 ⇒ 〈x0,x4〉
334 | MODE_IMM2 ⇒ 〈x0,x5〉
335 | MODE_DIR1 ⇒ 〈x0,x6〉
336 | MODE_DIR2 ⇒ 〈x0,x7〉
343 | MODE_DIR1_to_DIR1 ⇒ 〈x0,xD〉
344 | MODE_IMM1_to_DIR1 ⇒ 〈x0,xE〉
345 | MODE_IX0p_to_DIR1 ⇒ 〈x0,xF〉
346 | MODE_DIR1_to_IX0p ⇒ 〈x1,x0〉
348 | MODE_INHA_and_IMM1 ⇒ 〈x1,x1〉
349 | MODE_INHX_and_IMM1 ⇒ 〈x1,x2〉
350 | MODE_IMM1_and_IMM1 ⇒ 〈x1,x3〉
351 | MODE_DIR1_and_IMM1 ⇒ 〈x1,x4〉
352 | MODE_IX0_and_IMM1 ⇒ 〈x1,x5〉
353 | MODE_IX0p_and_IMM1 ⇒ 〈x1,x6〉
354 | MODE_IX1_and_IMM1 ⇒ 〈x1,x7〉
355 | MODE_IX1p_and_IMM1 ⇒ 〈x1,x8〉
356 | MODE_SP1_and_IMM1 ⇒ 〈x1,x9〉
358 (* 26-33: bisogna considerare l'operando implicito *)
359 | MODE_DIRn o ⇒ plus_b8nc 〈x1,xA〉 〈x0,(exadecim_of_oct o)〉
360 (* 34-41: bisogna considerare l'operando implicito *)
361 | MODE_DIRn_and_IMM1 o ⇒ plus_b8nc 〈x2,x2〉 〈x0,(exadecim_of_oct o)〉
362 (* 42-57: bisogna considerare l'operando implicito *)
363 | MODE_TNY e ⇒ plus_b8nc 〈x2,xA〉 〈x0,e〉
364 (* 58-99: bisogna considerare gli operandi impliciti *)
365 | MODE_SRT t ⇒ plus_b8nc 〈x3,xA〉 (byte8_of_bitrigesim t)
368 (* confronto fra instr_mode *)
370 λi:instr_mode.λi':instr_mode.(eq_b8 (magic_of_instr_mode i) (magic_of_instr_mode i')).
372 (* ********************************************* *)
373 (* STRUMENTI PER LE DIMOSTRAZIONI DI CORRETTEZZA *)
374 (* ********************************************* *)
376 (* su tutta la lista quante volte compare il byte *)
377 let rec get_byte_count (m:mcu_type) (b:byte8) (c:nat)
378 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
381 | cons hd tl ⇒ match thd4T ???? hd with
382 [ Byte b' ⇒ match eq_b8 b b' with
383 [ true ⇒ get_byte_count m b (S c) tl
384 | false ⇒ get_byte_count m b c tl
386 | Word _ ⇒ get_byte_count m b c tl
390 (* su tutta la lista quante volte compare la word (0x9E+byte) *)
391 let rec get_word_count (m:mcu_type) (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 m b c tl
397 | Word w ⇒ match eq_w16 〈〈x9,xE〉:b〉 w with
398 [ true ⇒ get_word_count m b (S c) tl
399 | false ⇒ get_word_count m b c tl
404 (* b e' non implementato? *)
405 let rec test_not_impl (b:byte8) (l:list byte8) on l ≝
408 | cons hd tl ⇒ match eq_b8 b hd with
410 | false ⇒ test_not_impl b tl
414 (* su tutta la lista quante volte compare la coppia opcode,instr_mode *)
415 let rec get_OpIm_count (m:mcu_type) (o:any_opcode m) (i:instr_mode) (c:nat)
416 (l:list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)) on l ≝
420 match (eqop m o (fst4T ???? hd)) ⊗
421 (eqim i (snd4T ???? hd)) with
422 [ true ⇒ get_OpIm_count m o i (S c) tl
423 | false ⇒ get_OpIm_count m o i c tl
427 (* iteratore sugli opcode *)
428 definition forall_opcode ≝ λP.
429 P ADC ⊗ P ADD ⊗ P AIS ⊗ P AIX ⊗ P AND ⊗ P ASL ⊗ P ASR ⊗ P BCC ⊗
430 P BCLRn ⊗ P BCS ⊗ P BEQ ⊗ P BGE ⊗ P BGND ⊗ P BGT ⊗ P BHCC ⊗ P BHCS ⊗
431 P BHI ⊗ P BIH ⊗ P BIL ⊗ P BIT ⊗ P BLE ⊗ P BLS ⊗ P BLT ⊗ P BMC ⊗
432 P BMI ⊗ P BMS ⊗ P BNE ⊗ P BPL ⊗ P BRA ⊗ P BRCLRn ⊗ P BRN ⊗ P BRSETn ⊗
433 P BSETn ⊗ P BSR ⊗ P CBEQA ⊗ P CBEQX ⊗ P CLC ⊗ P CLI ⊗ P CLR ⊗ P CMP ⊗
434 P COM ⊗ P CPHX ⊗ P CPX ⊗ P DAA ⊗ P DBNZ ⊗ P DEC ⊗ P DIV ⊗ P EOR ⊗
435 P INC ⊗ P JMP ⊗ P JSR ⊗ P LDA ⊗ P LDHX ⊗ P LDX ⊗ P LSR ⊗ P MOV ⊗
436 P MUL ⊗ P NEG ⊗ P NOP ⊗ P NSA ⊗ P ORA ⊗ P PSHA ⊗ P PSHH ⊗ P PSHX ⊗
437 P PULA ⊗ P PULH ⊗ P PULX ⊗ P ROL ⊗ P ROR ⊗ P RSP ⊗ P RTI ⊗ P RTS ⊗
438 P SBC ⊗ P SEC ⊗ P SEI ⊗ P SHA ⊗ P SLA ⊗ P STA ⊗ P STHX ⊗ P STOP ⊗
439 P STX ⊗ P SUB ⊗ P SWI ⊗ P TAP ⊗ P TAX ⊗ P TPA ⊗ P TST ⊗ P TSX ⊗
440 P TXA ⊗ P TXS ⊗ P WAIT.
442 (* iteratore sulle modalita' *)
443 definition forall_instr_mode ≝ λP.
459 ⊗ P MODE_DIR1_to_DIR1
460 ⊗ P MODE_IMM1_to_DIR1
461 ⊗ P MODE_IX0p_to_DIR1
462 ⊗ P MODE_DIR1_to_IX0p
464 ⊗ P MODE_INHA_and_IMM1
465 ⊗ P MODE_INHX_and_IMM1
466 ⊗ P MODE_IMM1_and_IMM1
467 ⊗ P MODE_DIR1_and_IMM1
468 ⊗ P MODE_IX0_and_IMM1
469 ⊗ P MODE_IX0p_and_IMM1
470 ⊗ P MODE_IX1_and_IMM1
471 ⊗ P MODE_IX1p_and_IMM1
472 ⊗ P MODE_SP1_and_IMM1
474 ⊗ forall_oct (λo. P (MODE_DIRn o))
475 ⊗ forall_oct (λo. P (MODE_DIRn_and_IMM1 o))
476 ⊗ forall_exadecim (λe. P (MODE_TNY e))
477 ⊗ forall_bitrigesim (λt. P (MODE_SRT t)).