From: Daniel Elstner Date: Thu, 27 Feb 2014 20:31:10 +0000 (+0100) Subject: z80: Format hex numbers with leading zero if necessary. X-Git-Tag: libsigrokdecode-0.3.0~74 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=aef3c1095e966212fd9a9b02cb08a0ff50e13a7b;p=libsigrokdecode.git z80: Format hex numbers with leading zero if necessary. Assembler syntax requires that all numbers start with a decimal digit. Introduce a custom 'H' format for prefixing a leading 0 to hexadecimal numbers that would otherwise start with a letter. --- diff --git a/decoders/z80/pd.py b/decoders/z80/pd.py index cd58b48..088408a 100644 --- a/decoders/z80/pd.py +++ b/decoders/z80/pd.py @@ -20,6 +20,7 @@ import sigrokdecode as srd from functools import reduce from .tables import instr_table_by_prefix +import string class Ann: ADDR, MEMRD, MEMWR, IORD, IOWR, INSTR, ROP, WOP, WARN = range(9) @@ -47,6 +48,18 @@ class OpState: WOP2 = 'WOP2' # second byte of write operand RESTART = 'RESTART' # restart instruction decoding +# Provide custom format type 'H' for hexadecimal output +# with leading decimal digit (assembler syntax). +class AsmFormatter(string.Formatter): + def format_field(self, value, format_spec): + if format_spec.endswith('H'): + result = format(value, format_spec[:-1] + 'X') + return result if result[0] in string.digits else '0' + result + else: + return format(value, format_spec) + +formatter = AsmFormatter() + ann_data_cycle_map = { Cycle.MEMRD: Ann.MEMRD, Cycle.MEMWR: Ann.MEMWR, @@ -183,9 +196,9 @@ class Decoder(srd.Decoder): self.ann_dasm = None def put_disasm(self): - text = self.mnemonic.format(r=self.arg_reg, d=self.arg_dis, - i=self.arg_imm, ro=self.arg_read, - wo=self.arg_write) + text = formatter.format(self.mnemonic, r=self.arg_reg, + d=self.arg_dis, i=self.arg_imm, + ro=self.arg_read, wo=self.arg_write) self.put_text(self.dasm_start, self.ann_dasm, text) self.ann_dasm = None self.dasm_start = self.samplenum diff --git a/decoders/z80/tables.py b/decoders/z80/tables.py index b0d9e52..3f768b2 100644 --- a/decoders/z80/tables.py +++ b/decoders/z80/tables.py @@ -40,12 +40,12 @@ Instruction tuple: (d, i, ro, wo, rep, format string) # Instructions without a prefix main_instructions = { 0x00: (0, 0, 0, 0, False, 'NOP'), - 0x01: (0, 2, 0, 0, False, 'LD BC,{i:04X}h'), + 0x01: (0, 2, 0, 0, False, 'LD BC,{i:04H}h'), 0x02: (0, 0, 0, 1, False, 'LD (BC),A'), 0x03: (0, 0, 0, 0, False, 'INC BC'), 0x04: (0, 0, 0, 0, False, 'INC B'), 0x05: (0, 0, 0, 0, False, 'DEC B'), - 0x06: (0, 1, 0, 0, False, 'LD B,{i:02X}h'), + 0x06: (0, 1, 0, 0, False, 'LD B,{i:02H}h'), 0x07: (0, 0, 0, 0, False, 'RLCA'), 0x08: (0, 0, 0, 0, False, 'EX AF,AF\''), 0x09: (0, 0, 0, 0, False, 'ADD HL,BC'), @@ -53,16 +53,16 @@ main_instructions = { 0x0B: (0, 0, 0, 0, False, 'DEC BC'), 0x0C: (0, 0, 0, 0, False, 'INC C'), 0x0D: (0, 0, 0, 0, False, 'DEC C'), - 0x0E: (0, 1, 0, 0, False, 'LD C,{i:02X}h'), + 0x0E: (0, 1, 0, 0, False, 'LD C,{i:02H}h'), 0x0F: (0, 0, 0, 0, False, 'RRCA'), 0x10: (1, 0, 0, 0, False, 'DJNZ {d:+d}'), - 0x11: (0, 2, 0, 0, False, 'LD DE,{i:04X}h'), + 0x11: (0, 2, 0, 0, False, 'LD DE,{i:04H}h'), 0x12: (0, 0, 0, 1, False, 'LD (DE),A'), 0x13: (0, 0, 0, 0, False, 'INC DE'), 0x14: (0, 0, 0, 0, False, 'INC D'), 0x15: (0, 0, 0, 0, False, 'DEC D'), - 0x16: (0, 1, 0, 0, False, 'LD D,{i:02X}h'), + 0x16: (0, 1, 0, 0, False, 'LD D,{i:02H}h'), 0x17: (0, 0, 0, 0, False, 'RLA'), 0x18: (1, 0, 0, 0, False, 'JR {d:+d}'), 0x19: (0, 0, 0, 0, False, 'ADD HL,DE'), @@ -70,41 +70,41 @@ main_instructions = { 0x1B: (0, 0, 0, 0, False, 'DEC DE'), 0x1C: (0, 0, 0, 0, False, 'INC E'), 0x1D: (0, 0, 0, 0, False, 'DEC E'), - 0x1E: (0, 1, 0, 0, False, 'LD E,{i:02X}h'), + 0x1E: (0, 1, 0, 0, False, 'LD E,{i:02H}h'), 0x1F: (0, 0, 0, 0, False, 'RRA'), 0x20: (1, 0, 0, 0, False, 'JR NZ,{d:+d}'), - 0x21: (0, 2, 0, 0, False, 'LD HL,{i:04X}h'), - 0x22: (0, 2, 0, 2, False, 'LD ({i:04X}h),HL'), + 0x21: (0, 2, 0, 0, False, 'LD HL,{i:04H}h'), + 0x22: (0, 2, 0, 2, False, 'LD ({i:04H}h),HL'), 0x23: (0, 0, 0, 0, False, 'INC HL'), 0x24: (0, 0, 0, 0, False, 'INC H'), 0x25: (0, 0, 0, 0, False, 'DEC H'), - 0x26: (0, 1, 0, 0, False, 'LD H,{i:02X}h'), + 0x26: (0, 1, 0, 0, False, 'LD H,{i:02H}h'), 0x27: (0, 0, 0, 0, False, 'DAA'), 0x28: (1, 0, 0, 0, False, 'JR Z,{d:+d}'), 0x29: (0, 0, 0, 0, False, 'ADD HL,HL'), - 0x2A: (0, 2, 2, 0, False, 'LD HL,({i:04X}h)'), + 0x2A: (0, 2, 2, 0, False, 'LD HL,({i:04H}h)'), 0x2B: (0, 0, 0, 0, False, 'DEC HL'), 0x2C: (0, 0, 0, 0, False, 'INC L'), 0x2D: (0, 0, 0, 0, False, 'DEC L'), - 0x2E: (0, 1, 0, 0, False, 'LD L,{i:02X}h'), + 0x2E: (0, 1, 0, 0, False, 'LD L,{i:02H}h'), 0x2F: (0, 0, 0, 0, False, 'CPL'), 0x30: (1, 0, 0, 0, False, 'JR NC,{d:+d}'), - 0x31: (0, 2, 0, 0, False, 'LD SP,{i:04X}h'), - 0x32: (0, 2, 0, 1, False, 'LD ({i:04X}h),A'), + 0x31: (0, 2, 0, 0, False, 'LD SP,{i:04H}h'), + 0x32: (0, 2, 0, 1, False, 'LD ({i:04H}h),A'), 0x33: (0, 0, 0, 0, False, 'INC SP'), 0x34: (0, 0, 1, 1, False, 'INC (HL)'), 0x35: (0, 0, 1, 1, False, 'DEC (HL)'), - 0x36: (0, 1, 0, 1, False, 'LD (HL),{i:02X}h'), + 0x36: (0, 1, 0, 1, False, 'LD (HL),{i:02H}h'), 0x37: (0, 0, 0, 0, False, 'SCF'), 0x38: (1, 0, 0, 0, False, 'JR C,{d:+d}'), 0x39: (0, 0, 0, 0, False, 'ADD HL,SP'), - 0x3A: (0, 2, 1, 0, False, 'LD A,({i:04X}h)'), + 0x3A: (0, 2, 1, 0, False, 'LD A,({i:04H}h)'), 0x3B: (0, 0, 0, 0, False, 'DEC SP'), 0x3C: (0, 0, 0, 0, False, 'INC A'), 0x3D: (0, 0, 0, 0, False, 'DEC A'), - 0x3E: (0, 1, 0, 0, False, 'LD A,{i:02X}h'), + 0x3E: (0, 1, 0, 0, False, 'LD A,{i:02H}h'), 0x3F: (0, 0, 0, 0, False, 'CCF'), 0x40: (0, 0, 0, 0, False, 'LD B,B'), @@ -245,70 +245,70 @@ main_instructions = { 0xC0: (0, 0, 2, 0, False, 'RET NZ'), 0xC1: (0, 0, 2, 0, False, 'POP BC'), - 0xC2: (0, 2, 0, 0, False, 'JP NZ,{i:04X}h'), - 0xC3: (0, 2, 0, 0, False, 'JP {i:04X}h'), - 0xC4: (0, 2, 0,-2, False, 'CALL NZ,{i:04X}h'), + 0xC2: (0, 2, 0, 0, False, 'JP NZ,{i:04H}h'), + 0xC3: (0, 2, 0, 0, False, 'JP {i:04H}h'), + 0xC4: (0, 2, 0,-2, False, 'CALL NZ,{i:04H}h'), 0xC5: (0, 0, 0,-2, False, 'PUSH BC'), - 0xC6: (0, 1, 0, 0, False, 'ADD A,{i:02X}h'), + 0xC6: (0, 1, 0, 0, False, 'ADD A,{i:02H}h'), 0xC7: (0, 0, 0,-2, False, 'RST 00h'), 0xC8: (0, 0, 2, 0, False, 'RET Z'), 0xC9: (0, 0, 2, 0, False, 'RET'), - 0xCA: (0, 2, 0, 0, False, 'JP Z,{i:04X}h'), + 0xCA: (0, 2, 0, 0, False, 'JP Z,{i:04H}h'), - 0xCC: (0, 2, 0,-2, False, 'CALL Z,{i:04X}h'), - 0xCD: (0, 2, 0,-2, False, 'CALL {i:04X}h'), - 0xCE: (0, 1, 0, 0, False, 'ADC A,{i:02X}h'), + 0xCC: (0, 2, 0,-2, False, 'CALL Z,{i:04H}h'), + 0xCD: (0, 2, 0,-2, False, 'CALL {i:04H}h'), + 0xCE: (0, 1, 0, 0, False, 'ADC A,{i:02H}h'), 0xCF: (0, 0, 0,-2, False, 'RST 08h'), 0xD0: (0, 0, 2, 0, False, 'RET NC'), 0xD1: (0, 0, 2, 0, False, 'POP DE'), - 0xD2: (0, 2, 0, 0, False, 'JP NC,{i:04X}h'), - 0xD3: (0, 1, 0, 1, False, 'OUT ({i:02X}h),A'), - 0xD4: (0, 2, 0,-2, False, 'CALL NC,{i:04X}h'), + 0xD2: (0, 2, 0, 0, False, 'JP NC,{i:04H}h'), + 0xD3: (0, 1, 0, 1, False, 'OUT ({i:02H}h),A'), + 0xD4: (0, 2, 0,-2, False, 'CALL NC,{i:04H}h'), 0xD5: (0, 0, 0,-2, False, 'PUSH DE'), - 0xD6: (0, 1, 0, 0, False, 'SUB {i:02X}h'), + 0xD6: (0, 1, 0, 0, False, 'SUB {i:02H}h'), 0xD7: (0, 0, 0,-2, False, 'RST 10h'), 0xD8: (0, 0, 2, 0, False, 'RET C'), 0xD9: (0, 0, 0, 0, False, 'EXX'), - 0xDA: (0, 2, 0, 0, False, 'JP C,{i:04X}h'), - 0xDB: (0, 1, 1, 0, False, 'IN A,({i:02X}h)'), - 0xDC: (0, 2, 0,-2, False, 'CALL C,{i:04X}h'), + 0xDA: (0, 2, 0, 0, False, 'JP C,{i:04H}h'), + 0xDB: (0, 1, 1, 0, False, 'IN A,({i:02H}h)'), + 0xDC: (0, 2, 0,-2, False, 'CALL C,{i:04H}h'), - 0xDE: (0, 1, 0, 0, False, 'SBC A,{i:02X}h'), + 0xDE: (0, 1, 0, 0, False, 'SBC A,{i:02H}h'), 0xDF: (0, 0, 0,-2, False, 'RST 18h'), 0xE0: (0, 0, 2, 0, False, 'RET PO'), 0xE1: (0, 0, 2, 0, False, 'POP HL'), - 0xE2: (0, 2, 0, 0, False, 'JP PO,{i:04X}h'), + 0xE2: (0, 2, 0, 0, False, 'JP PO,{i:04H}h'), 0xE3: (0, 0, 2, 2, False, 'EX (SP),HL'), - 0xE4: (0, 2, 0,-2, False, 'CALL PO,{i:04X}h'), + 0xE4: (0, 2, 0,-2, False, 'CALL PO,{i:04H}h'), 0xE5: (0, 0, 0,-2, False, 'PUSH HL'), - 0xE6: (0, 1, 0, 0, False, 'AND {i:02X}h'), + 0xE6: (0, 1, 0, 0, False, 'AND {i:02H}h'), 0xE7: (0, 0, 0,-2, False, 'RST 20h'), 0xE8: (0, 0, 2, 0, False, 'RET PE'), 0xE9: (0, 0, 0, 0, False, 'JP (HL)'), - 0xEA: (0, 2, 0, 0, False, 'JP PE,{i:04X}h'), + 0xEA: (0, 2, 0, 0, False, 'JP PE,{i:04H}h'), 0xEB: (0, 0, 0, 0, False, 'EX DE,HL'), - 0xEC: (0, 2, 0,-2, False, 'CALL PE,{i:04X}h'), + 0xEC: (0, 2, 0,-2, False, 'CALL PE,{i:04H}h'), - 0xEE: (0, 1, 0, 0, False, 'XOR {i:02X}h'), + 0xEE: (0, 1, 0, 0, False, 'XOR {i:02H}h'), 0xEF: (0, 0, 0,-2, False, 'RST 28h'), 0xF0: (0, 0, 2, 0, False, 'RET P'), 0xF1: (0, 0, 2, 0, False, 'POP AF'), - 0xF2: (0, 2, 0, 0, False, 'JP P,{i:04X}h'), + 0xF2: (0, 2, 0, 0, False, 'JP P,{i:04H}h'), 0xF3: (0, 0, 0, 0, False, 'DI'), - 0xF4: (0, 2, 0,-2, False, 'CALL P,{i:04X}h'), + 0xF4: (0, 2, 0,-2, False, 'CALL P,{i:04H}h'), 0xF5: (0, 0, 0,-2, False, 'PUSH AF'), - 0xF6: (0, 1, 0, 0, False, 'OR {i:02X}h'), + 0xF6: (0, 1, 0, 0, False, 'OR {i:02H}h'), 0xF7: (0, 0, 0,-2, False, 'RST 30h'), 0xF8: (0, 0, 2, 0, False, 'RET M'), 0xF9: (0, 0, 0, 0, False, 'LD SP,HL'), - 0xFA: (0, 2, 0, 0, False, 'JP M,{i:04X}h'), + 0xFA: (0, 2, 0, 0, False, 'JP M,{i:04H}h'), 0xFB: (0, 0, 0, 0, False, 'EI'), - 0xFC: (0, 2, 0,-2, False, 'CALL M,{i:04X}h'), + 0xFC: (0, 2, 0,-2, False, 'CALL M,{i:04H}h'), - 0xFE: (0, 1, 0, 0, False, 'CP {i:02X}h'), + 0xFE: (0, 1, 0, 0, False, 'CP {i:02H}h'), 0xFF: (0, 0, 0,-2, False, 'RST 38h') } @@ -317,7 +317,7 @@ extended_instructions = { 0x40: (0, 0, 1, 0, False, 'IN B,(C)'), 0x41: (0, 0, 0, 1, False, 'OUT (C),B'), 0x42: (0, 0, 0, 0, False, 'SBC HL,BC'), - 0x43: (0, 2, 0, 2, False, 'LD ({i:04X}h),BC'), + 0x43: (0, 2, 0, 2, False, 'LD ({i:04H}h),BC'), 0x44: (0, 0, 0, 0, False, 'NEG'), 0x45: (0, 0, 2, 0, False, 'RETN'), 0x46: (0, 0, 0, 0, False, 'IM 0'), @@ -325,7 +325,7 @@ extended_instructions = { 0x48: (0, 0, 1, 0, False, 'IN C,(C)'), 0x49: (0, 0, 0, 1, False, 'OUT (C),C'), 0x4A: (0, 0, 0, 0, False, 'ADC HL,BC'), - 0x4B: (0, 2, 2, 0, False, 'LD BC,({i:04X}h)'), + 0x4B: (0, 2, 2, 0, False, 'LD BC,({i:04H}h)'), 0x4C: (0, 0, 0, 0, False, 'NEG'), 0x4D: (0, 0, 2, 0, False, 'RETI'), 0x4E: (0, 0, 0, 0, False, 'IM 0/1'), @@ -334,7 +334,7 @@ extended_instructions = { 0x50: (0, 0, 1, 0, False, 'IN D,(C)'), 0x51: (0, 0, 0, 1, False, 'OUT (C),D'), 0x52: (0, 0, 0, 0, False, 'SBC HL,DE'), - 0x53: (0, 2, 0, 2, False, 'LD ({i:04X}h),DE'), + 0x53: (0, 2, 0, 2, False, 'LD ({i:04H}h),DE'), 0x54: (0, 0, 0, 0, False, 'NEG'), 0x55: (0, 0, 2, 0, False, 'RETN'), 0x56: (0, 0, 0, 0, False, 'IM 1'), @@ -342,7 +342,7 @@ extended_instructions = { 0x58: (0, 0, 1, 0, False, 'IN E,(C)'), 0x59: (0, 0, 0, 1, False, 'OUT (C),E'), 0x5A: (0, 0, 0, 0, False, 'ADC HL,DE'), - 0x5B: (0, 2, 2, 0, False, 'LD DE,({i:04X}h)'), + 0x5B: (0, 2, 2, 0, False, 'LD DE,({i:04H}h)'), 0x5C: (0, 0, 0, 0, False, 'NEG'), 0x5D: (0, 0, 2, 0, False, 'RETN'), 0x5E: (0, 0, 0, 0, False, 'IM 2'), @@ -351,7 +351,7 @@ extended_instructions = { 0x60: (0, 0, 1, 0, False, 'IN H,(C)'), 0x61: (0, 0, 0, 1, False, 'OUT (C),H'), 0x62: (0, 0, 0, 0, False, 'SBC HL,HL'), - 0x63: (0, 2, 0, 2, False, 'LD ({i:04X}h),HL'), + 0x63: (0, 2, 0, 2, False, 'LD ({i:04H}h),HL'), 0x64: (0, 0, 0, 0, False, 'NEG'), 0x65: (0, 0, 2, 0, False, 'RETN'), 0x66: (0, 0, 0, 0, False, 'IM 0'), @@ -359,7 +359,7 @@ extended_instructions = { 0x68: (0, 0, 1, 0, False, 'IN L,(C)'), 0x69: (0, 0, 0, 1, False, 'OUT (C),L'), 0x6A: (0, 0, 0, 0, False, 'ADC HL,HL'), - 0x6B: (0, 2, 2, 0, False, 'LD HL,({i:04X}h)'), + 0x6B: (0, 2, 2, 0, False, 'LD HL,({i:04H}h)'), 0x6C: (0, 0, 0, 0, False, 'NEG'), 0x6D: (0, 0, 2, 0, False, 'RETN'), 0x6E: (0, 0, 0, 0, False, 'IM 0/1'), @@ -368,7 +368,7 @@ extended_instructions = { 0x70: (0, 0, 1, 0, False, 'IN (C)'), 0x71: (0, 0, 0, 1, False, 'OUT (C),0'), 0x72: (0, 0, 0, 0, False, 'SBC HL,SP'), - 0x73: (0, 2, 0, 2, False, 'LD ({i:04X}h),SP'), + 0x73: (0, 2, 0, 2, False, 'LD ({i:04H}h),SP'), 0x74: (0, 0, 0, 0, False, 'NEG'), 0x75: (0, 0, 2, 0, False, 'RETN'), 0x76: (0, 0, 0, 0, False, 'IM 1'), @@ -376,7 +376,7 @@ extended_instructions = { 0x78: (0, 0, 1, 0, False, 'IN A,(C)'), 0x79: (0, 0, 0, 1, False, 'OUT (C),A'), 0x7A: (0, 0, 0, 0, False, 'ADC HL,SP'), - 0x7B: (0, 2, 2, 0, False, 'LD SP,({i:04X}h)'), + 0x7B: (0, 2, 2, 0, False, 'LD SP,({i:04H}h)'), 0x7C: (0, 0, 0, 0, False, 'NEG'), 0x7D: (0, 0, 2, 0, False, 'RETN'), 0x7E: (0, 0, 0, 0, False, 'IM 2'), @@ -683,23 +683,23 @@ index_instructions = { 0x19: (0, 0, 0, 0, False, 'ADD {r},DE'), - 0x21: (0, 2, 0, 0, False, 'LD {r},{i:04X}h'), - 0x22: (0, 2, 0, 2, False, 'LD ({i:04X}h),{r}'), + 0x21: (0, 2, 0, 0, False, 'LD {r},{i:04H}h'), + 0x22: (0, 2, 0, 2, False, 'LD ({i:04H}h),{r}'), 0x23: (0, 0, 0, 0, False, 'INC {r}'), 0x24: (0, 0, 0, 0, False, 'INC {r}h'), 0x25: (0, 0, 0, 0, False, 'DEC {r}h'), - 0x26: (0, 1, 0, 0, False, 'LD {r}h,{i:02X}h'), + 0x26: (0, 1, 0, 0, False, 'LD {r}h,{i:02H}h'), 0x29: (0, 0, 0, 0, False, 'ADD {r},{r}'), - 0x2A: (0, 2, 2, 0, False, 'LD {r},({i:04X}h)'), + 0x2A: (0, 2, 2, 0, False, 'LD {r},({i:04H}h)'), 0x2B: (0, 0, 0, 0, False, 'DEC {r}'), 0x2C: (0, 0, 0, 0, False, 'INC {r}l'), 0x2D: (0, 0, 0, 0, False, 'DEC {r}l'), - 0x2E: (0, 1, 0, 0, False, 'LD {r}l,{i:02X}h'), + 0x2E: (0, 1, 0, 0, False, 'LD {r}l,{i:02H}h'), 0x34: (1, 0, 1, 1, False, 'INC ({r}{d:+d})'), 0x35: (1, 0, 1, 1, False, 'DEC ({r}{d:+d})'), - 0x36: (1, 1, 0, 1, False, 'LD ({r}{d:+d}),{i:02X}h'), + 0x36: (1, 1, 0, 1, False, 'LD ({r}{d:+d}),{i:02H}h'), 0x39: (0, 0, 0, 0, False, 'ADD {r},SP'),