[120] | 1 | open BitVectors;; |
---|
| 2 | |
---|
[27] | 3 | type ('a,'b) union2 = [ `U1 of 'a | `U2 of 'b ] |
---|
| 4 | type ('a,'b,'c) union3 = [ `U1 of 'a | `U2 of 'b | `U3 of 'c ] |
---|
| 5 | type ('a,'b,'c,'d,'e,'f) union6 = [ `U1 of 'a | `U2 of 'b | `U3 of 'c | `U4 of 'd | `U5 of 'e | `U6 of 'f ] |
---|
| 6 | |
---|
[28] | 7 | type direct = [ `DIRECT of byte ] |
---|
[46] | 8 | type indirect = [ `INDIRECT of bit ] |
---|
| 9 | type ext_indirect = [ `EXT_INDIRECT of bit ] |
---|
[28] | 10 | type reg = [ `REG of bit * bit * bit ] |
---|
[27] | 11 | type acc = [ `A ] |
---|
| 12 | type b = [ `B ] |
---|
| 13 | type dptr = [ `DPTR ] |
---|
| 14 | type data = [ `DATA of byte ] |
---|
| 15 | type data16 = [ `DATA16 of word ] |
---|
| 16 | type acc_dptr = [ `A_DPTR ] |
---|
| 17 | type acc_pc = [ `A_PC ] |
---|
[158] | 18 | type ext_indirect_dptr = [ `EXT_IND_DPTR ] |
---|
[27] | 19 | type indirect_dptr = [ `IND_DPTR ] |
---|
| 20 | type carry = [ `C ] |
---|
[46] | 21 | type bit = [ `BIT of byte ] |
---|
| 22 | type nbit = [ `NBIT of byte ] |
---|
[27] | 23 | type rel = [ `REL of byte ] |
---|
| 24 | type addr11 = [ `ADDR11 of word11 ] |
---|
| 25 | type addr16 = [ `ADDR16 of word ] |
---|
| 26 | |
---|
[200] | 27 | type 'addr jump = |
---|
| 28 | [ `JC of 'addr |
---|
| 29 | | `JNC of 'addr |
---|
| 30 | | `JB of bit * 'addr |
---|
| 31 | | `JNB of bit * 'addr |
---|
| 32 | | `JBC of bit * 'addr |
---|
| 33 | | `JZ of 'addr |
---|
| 34 | | `JNZ of 'addr |
---|
| 35 | | `CJNE of (acc * [ direct | data ], [ reg | indirect ] * data) union2 * 'addr |
---|
| 36 | | `DJNZ of [ reg | direct ] * 'addr |
---|
| 37 | ] |
---|
| 38 | |
---|
| 39 | type 'addr preinstruction = |
---|
[27] | 40 | (* arithmetic operations *) |
---|
[97] | 41 | [ `ADD of acc * [ reg | direct | indirect | data ] |
---|
| 42 | | `ADDC of acc * [ reg | direct | indirect | data ] |
---|
| 43 | | `SUBB of acc * [ reg | direct | indirect | data ] |
---|
| 44 | | `INC of [ acc | reg | direct | indirect | dptr ] |
---|
| 45 | | `DEC of [ acc | reg | direct | indirect ] |
---|
| 46 | | `MUL of acc * b |
---|
| 47 | | `DIV of acc * b |
---|
| 48 | | `DA of acc |
---|
[27] | 49 | |
---|
| 50 | (* logical operations *) |
---|
[97] | 51 | | `ANL of |
---|
[28] | 52 | (acc * [ reg | direct | indirect | data ], |
---|
[27] | 53 | direct * [ acc | data ], |
---|
| 54 | carry * [ bit | nbit]) union3 |
---|
[97] | 55 | | `ORL of |
---|
[35] | 56 | (acc * [ reg | data | direct | indirect ], |
---|
[27] | 57 | direct * [ acc | data ], |
---|
| 58 | carry * [ bit | nbit]) union3 |
---|
[97] | 59 | | `XRL of |
---|
[35] | 60 | (acc * [ data | reg | direct | indirect ], |
---|
[27] | 61 | direct * [ acc | data ]) union2 |
---|
[97] | 62 | | `CLR of [ acc | carry | bit ] |
---|
| 63 | | `CPL of [ acc | carry | bit ] |
---|
| 64 | | `RL of acc |
---|
| 65 | | `RLC of acc |
---|
| 66 | | `RR of acc |
---|
| 67 | | `RRC of acc |
---|
| 68 | | `SWAP of acc |
---|
[27] | 69 | |
---|
| 70 | (* data transfer *) |
---|
[97] | 71 | | `MOV of |
---|
[27] | 72 | (acc * [ reg | direct | indirect | data ], |
---|
| 73 | [ reg | indirect ] * [ acc | direct | data ], |
---|
| 74 | direct * [ acc | reg | direct | indirect | data ], |
---|
| 75 | dptr * data16, |
---|
| 76 | carry * bit, |
---|
| 77 | bit * carry |
---|
| 78 | ) union6 |
---|
[97] | 79 | | `MOVC of acc * [ acc_dptr | acc_pc ] |
---|
| 80 | | `MOVX of (acc * [ ext_indirect | ext_indirect_dptr ], |
---|
[46] | 81 | [ ext_indirect | ext_indirect_dptr ] * acc) union2 |
---|
[97] | 82 | | `SETB of [ carry | bit ] |
---|
| 83 | | `PUSH of direct |
---|
| 84 | | `POP of direct |
---|
| 85 | | `XCH of acc * [ reg | direct | indirect ] |
---|
| 86 | | `XCHD of acc * indirect |
---|
[27] | 87 | |
---|
| 88 | (* program branching *) |
---|
[200] | 89 | | 'addr jump |
---|
[97] | 90 | | `ACALL of addr11 |
---|
| 91 | | `LCALL of addr16 |
---|
| 92 | | `RET |
---|
| 93 | | `RETI |
---|
| 94 | | `AJMP of addr11 |
---|
| 95 | | `LJMP of addr16 |
---|
| 96 | | `SJMP of rel |
---|
| 97 | | `JMP of indirect_dptr |
---|
| 98 | | `NOP ] |
---|
| 99 | |
---|
[200] | 100 | type instruction = rel preinstruction |
---|
| 101 | |
---|
[97] | 102 | type labelled_instruction = |
---|
| 103 | [ instruction |
---|
[100] | 104 | | `Cost of string |
---|
[97] | 105 | | `Label of string |
---|
[100] | 106 | | `Jmp of string |
---|
| 107 | | `Call of string |
---|
[200] | 108 | | `Mov of dptr * string |
---|
| 109 | | `WithLabel of [`Label of string] jump |
---|
[100] | 110 | ] |
---|
[200] | 111 | |
---|
| 112 | (* pairs <datalabel,size> *) |
---|
| 113 | type preamble = (string * int) list |
---|
| 114 | |
---|
| 115 | type assembly_program = preamble * labelled_instruction list;; |
---|