]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/library/freescale/translation.ma
we added the classic substitution function
[helm.git] / helm / software / matita / library / freescale / translation.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 (* ********************************************************************** *)
16 (*                           Progetto FreeScale                           *)
17 (*                                                                        *)
18 (* Sviluppato da:                                                         *)
19 (*   Cosimo Oliboni, oliboni@cs.unibo.it                                  *)
20 (*                                                                        *)
21 (* Questo materiale fa parte della tesi:                                  *)
22 (*   "Formalizzazione Interattiva dei Microcontroller a 8bit FreeScale"   *)
23 (*                                                                        *)
24 (*                    data ultima modifica 15/11/2007                     *)
25 (* ********************************************************************** *)
26
27 set "baseuri" "cic:/matita/freescale/translation/".
28
29 (*include "/media/VIRTUOSO/freescale/table_HC05.ma".*)
30 include "freescale/table_HC05.ma".
31 (*include "/media/VIRTUOSO/freescale/table_HC08.ma".*)
32 include "freescale/table_HC08.ma".
33 (*include "/media/VIRTUOSO/freescale/table_HCS08.ma".*)
34 include "freescale/table_HCS08.ma".
35 (*include "/media/VIRTUOSO/freescale/table_RS08.ma".*)
36 include "freescale/table_RS08.ma".
37
38 (* ***************************** *)
39 (* TRADUZIONE ESADECIMALE → INFO *)
40 (* ***************************** *)
41
42 (* accesso alla tabella della ALU prescelta *)
43 definition opcode_table ≝
44 λm:mcu_type.
45  match m
46   return λm:mcu_type.list (Prod4T (any_opcode m) instr_mode byte8_or_word16 byte8)
47  with
48   [ HC05  ⇒ opcode_table_HC05
49   | HC08  ⇒ opcode_table_HC08
50   | HCS08 ⇒ opcode_table_HCS08
51   | RS08  ⇒ opcode_table_RS08
52  ].
53
54 (* traduzione mcu+esadecimale → info *)
55 (* NB: la ricerca per byte non matcha con una word con lo stesso byte superiore uguale *)
56 (* NB: per matchare una word (precode+code) bisogna passare una word *)
57 (* NB: il risultato e' sempre un'opzione, NON esiste un dummy opcode tipo UNKNOWN/ILLEGAL *)
58 definition full_info_of_word16 ≝
59 λm:mcu_type.λborw:byte8_or_word16.
60  let rec aux param ≝ match param with
61   [ nil ⇒ None ?
62   | cons hd tl ⇒
63    match thd4T ???? hd with
64     [ Byte b ⇒ match borw with
65      [ Byte borw' ⇒ match eq_b8 borw' b with
66       [ true ⇒ Some ? hd | false ⇒ aux tl ]
67      | Word _ ⇒ aux tl ]
68     | Word w ⇒ match borw with
69      [ Byte _ ⇒ aux tl
70      | Word borw' ⇒ match eq_w16 borw' w with
71       [ true ⇒ Some ? hd | false ⇒ aux tl ]            
72     ]]] in aux (opcode_table m).
73
74 (* ******************************************************* *)
75 (* TRADUZIONE MCU+OPCODE+MODALITA'+ARGOMENTI → ESADECIMALE *)
76 (* ******************************************************* *)
77
78 (* introduzione di un tipo byte8 dipendente dall'mcu_type (phantom type) *)
79 inductive t_byte8 (m:mcu_type) : Type ≝
80  TByte : byte8 → t_byte8 m.
81
82 coercion cic:/matita/freescale/translation/t_byte8.ind#xpointer(1/1/1).
83
84 (* introduzione di un tipo dipendente (dalla modalita') per gli argomenti *)
85 inductive MA_check : instr_mode → Type ≝
86   maINH              : MA_check MODE_INH
87 | maINHA             : MA_check MODE_INHA
88 | maINHX             : MA_check MODE_INHX
89 | maINHH             : MA_check MODE_INHH
90 | maIMM1             : byte8  → MA_check MODE_IMM1
91 | maIMM2             : word16 → MA_check MODE_IMM2
92 | maDIR1             : byte8  → MA_check MODE_DIR1
93 | maDIR2             : word16 → MA_check MODE_DIR2
94 | maIX0              : MA_check MODE_IX0
95 | maIX1              : byte8  → MA_check MODE_IX1
96 | maIX2              : word16 → MA_check MODE_IX2
97 | maSP1              : byte8  → MA_check MODE_SP1
98 | maSP2              : word16 → MA_check MODE_SP2
99 | maDIR1_to_DIR1     : byte8 → byte8 → MA_check MODE_DIR1_to_DIR1
100 | maIMM1_to_DIR1     : byte8 → byte8 → MA_check MODE_IMM1_to_DIR1
101 | maIX0p_to_DIR1     : byte8 → MA_check MODE_IX0p_to_DIR1
102 | maDIR1_to_IX0p     : byte8 → MA_check MODE_DIR1_to_IX0p
103 | maINHA_and_IMM1    : byte8 → MA_check MODE_INHA_and_IMM1
104 | maINHX_and_IMM1    : byte8 → MA_check MODE_INHX_and_IMM1
105 | maIMM1_and_IMM1    : byte8 → byte8 → MA_check MODE_IMM1_and_IMM1
106 | maDIR1_and_IMM1    : byte8 → byte8 → MA_check MODE_DIR1_and_IMM1
107 | maIX0_and_IMM1     : byte8 → MA_check MODE_IX0_and_IMM1
108 | maIX0p_and_IMM1    : byte8 → MA_check MODE_IX0p_and_IMM1
109 | maIX1_and_IMM1     : byte8 → byte8 → MA_check MODE_IX1_and_IMM1
110 | maIX1p_and_IMM1    : byte8 → byte8 → MA_check MODE_IX1p_and_IMM1
111 | maSP1_and_IMM1     : byte8 → byte8 → MA_check MODE_SP1_and_IMM1
112 | maDIRn             : ∀o.byte8 → MA_check (MODE_DIRn o)
113 | maDIRn_and_IMM1    : ∀o.byte8 → byte8 → MA_check (MODE_DIRn_and_IMM1 o)
114 | maTNY              : ∀e.MA_check (MODE_TNY e)
115 | maSRT              : ∀t.MA_check (MODE_SRT t)
116 .
117
118 (* tipo istruzione per unificare in una lista omogenea il sorgente *)
119 inductive instruction : Type ≝ 
120 instr: ∀i:instr_mode.opcode → MA_check i → instruction.
121
122 coercion cic:/matita/freescale/translation/instruction.ind#xpointer(1/1/1).
123
124 (* picker: trasforma l'argomento necessario in input a bytes_of_pseudo_instr_mode_param:
125    MA_check i → list (t_byte8 m) *)
126 definition args_picker ≝
127 λm:mcu_type.λi:instr_mode.λargs:MA_check i.
128  match args with
129   (* inherent: legale se nessun operando *) 
130   [ maINH    ⇒ nil ? 
131   | maINHA   ⇒ nil ? 
132   | maINHX   ⇒ nil ? 
133   | maINHH   ⇒ nil ?
134   (* _0/1/2: legale se nessun operando/1 byte/1 word *)
135   | maIMM1 b ⇒ [ TByte m b ]
136   | maIMM2 w ⇒ [ TByte m (w16h w); TByte m (w16l w) ]
137   | maDIR1 b ⇒ [ TByte m b ]
138   | maDIR2 w ⇒ [ TByte m (w16h w); TByte m (w16l w) ]
139   | maIX0    ⇒ nil ?
140   | maIX1 b  ⇒ [ TByte m b ]
141   | maIX2 w  ⇒ [ TByte m (w16h w); TByte m (w16l w) ]
142   | maSP1 b  ⇒ [ TByte m b ]
143   | maSP2 w  ⇒ [ TByte m (w16h w); TByte m (w16l w) ]
144   (* movimento: legale se 2 operandi byte *)
145   | maDIR1_to_DIR1 b1 b2  ⇒ [ TByte m b1 ; TByte m b2 ]
146   | maIMM1_to_DIR1 b1 b2  ⇒ [ TByte m b1 ; TByte m b2 ]
147   | maIX0p_to_DIR1 b      ⇒ [ TByte m b]
148   | maDIR1_to_IX0p b      ⇒ [ TByte m b]
149   (* cbeq/dbnz: legale se 1/2 operandi byte *)
150   | maINHA_and_IMM1 b     ⇒ [ TByte m b]
151   | maINHX_and_IMM1 b     ⇒ [ TByte m b]
152   | maIMM1_and_IMM1 b1 b2 ⇒ [ TByte m b1 ; TByte m b2 ]
153   | maDIR1_and_IMM1 b1 b2 ⇒ [ TByte m b1 ; TByte m b2 ]
154   | maIX0_and_IMM1  b     ⇒ [ TByte m b]
155   | maIX0p_and_IMM1 b     ⇒ [ TByte m b]
156   | maIX1_and_IMM1  b1 b2 ⇒ [ TByte m b1 ; TByte m b2 ]
157   | maIX1p_and_IMM1 b1 b2 ⇒ [ TByte m b1 ; TByte m b2 ]
158   | maSP1_and_IMM1  b1 b2 ⇒ [ TByte m b1 ; TByte m b2 ]
159   (* DIRn: legale se 1 operando byte *)
160   | maDIRn _ b ⇒ [ TByte m b]
161   (* DIRn_and_IMM1: legale se 2 operandi byte *)
162   | maDIRn_and_IMM1 _ b1 b2 ⇒ [ TByte m b1 ; TByte m b2 ]
163   (* TNY: legale se nessun operando *)
164   | maTNY _ ⇒ nil ?
165   (* SRT: legale se nessun operando *)
166   | maSRT _ ⇒ nil ?
167   ].
168
169 (* trasformatore finale: mcu+opcode+instr_mode+args → list (t_byte8 m) *)
170 definition bytes_of_pseudo_instr_mode_param ≝
171 λm:mcu_type.λo:any_opcode m.λi:instr_mode.λp:MA_check i.
172 let rec aux param  ≝ match param with
173  [ nil ⇒ None ? | cons hd tl ⇒
174   match (eqop m o (fst4T ???? hd)) ⊗ (eqim i (snd4T ???? hd)) with
175    [ true ⇒ match thd4T ???? hd with 
176     [ Byte isab ⇒ 
177      Some ? ([ (TByte m isab) ]@(args_picker m i p))
178     | Word isaw ⇒
179      Some ? ([ (TByte m (w16h isaw)) ; (TByte m (w16l isaw)) ]@(args_picker m i p))
180     ]
181    | false ⇒ aux tl ]] in aux (opcode_table m).
182
183 (* tipatore degli opcode generici *)
184 definition opcode_to_any ≝ λm:mcu_type.λo:opcode.anyOP m o.
185
186 (* ****************************** *)
187 (* APPROCCIO COMPILAZIONE AL VOLO *)
188 (* ****************************** *)
189
190 (* ausiliario di compile *)
191 definition defined ≝
192  λT:Type.λo:option T.
193   match o with
194    [ None ⇒ False
195    | Some _ ⇒ True
196    ].
197
198 (* compila solo se l'intera istruzione+modalita'+argomenti ha riscontro nelle tabelle *)
199 definition compile ≝
200 λmcu:mcu_type.λi:instr_mode.λop:opcode.λarg:MA_check i.
201  let res ≝ bytes_of_pseudo_instr_mode_param mcu (opcode_to_any mcu op) i arg in
202   λp:defined ? res.
203    let value ≝
204     match res return λres: option (list (t_byte8 mcu)).defined ? res → ? with
205     [ None ⇒ λp:defined (list (t_byte8 mcu)) (None ?).
206        False_rect ? p
207     | Some v ⇒ λ_:defined ? (Some ? v).v
208     ] p in value.
209
210 (* detipatore del compilato: (t_byte8 m) → byte8 *)
211 definition source_to_byte8 ≝
212 λmcu:mcu_type.
213  match mcu
214   return λmcu:mcu_type.list (t_byte8 mcu) → list byte8 with
215   [ HC05 ⇒ λl:list (t_byte8 HC05).
216    let rec aux (p1:list (t_byte8 HC05)) (p2:list byte8) ≝ match p1 with
217     [ nil ⇒ p2 | cons hd tl ⇒ match hd with [ TByte b ⇒ aux tl (p2@[b]) ]] in aux l ([])
218   | HC08 ⇒ λl:list (t_byte8 HC08).
219    let rec aux (p1:list (t_byte8 HC08)) (p2:list byte8) ≝ match p1 with
220     [ nil ⇒ p2 | cons hd tl ⇒ match hd with [ TByte b ⇒ aux tl (p2@[b]) ]] in aux l ([])
221   | HCS08 ⇒ λl:list (t_byte8 HCS08).
222    let rec aux (p1:list (t_byte8 HCS08)) (p2:list byte8) ≝ match p1 with
223     [ nil ⇒ p2 | cons hd tl ⇒ match hd with [ TByte b ⇒ aux tl (p2@[b]) ]] in aux l ([])
224   | RS08 ⇒ λl:list (t_byte8 RS08).
225    let rec aux (p1:list (t_byte8 RS08)) (p2:list byte8) ≝ match p1 with
226     [ nil ⇒ p2 | cons hd tl ⇒ match hd with [ TByte b ⇒ aux tl (p2@[b]) ]] in aux l ([])
227   ].
228
229 (* esempio da riciclare su come scrivere un sorgente *)
230 (*
231 definition source_example_of_correct_HC08: list byte8 ≝
232 let m ≝ HC08 in
233  source_to_byte8 m (
234   (compile m ? CLR maINHA I) @
235   (compile m ? NOP maINH I) @
236   (compile m ? BRSETn (maDIRn_and_IMM1 x1 〈x1,x2〉 〈x3,x4〉) I) @
237   (compile m ? ADD maIX0 I) @
238   (compile m ? ADD (maIX1 〈x1,x2〉) I) @
239   (compile m ? ADD (maSP2 〈〈x1,x2〉:〈x3,x4〉〉) I)
240  ).
241 *)