diff options
author | Ian C <ianc@noddybox.co.uk> | 2006-08-28 01:32:34 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2006-08-28 01:32:34 +0000 |
commit | 25738474b655bbff8c31b12634d7377520420baf (patch) | |
tree | 82fe6586ff721004d71386044cf3203f55f46936 | |
parent | 472447531bc02c0cbdf9793675ef4475fa07aef5 (diff) |
Added labels to Z80 disassembler
-rw-r--r-- | emma.c | 8 | ||||
-rw-r--r-- | z80.c | 7 | ||||
-rw-r--r-- | z80.h | 16 | ||||
-rw-r--r-- | z80_dis.c | 129 | ||||
-rw-r--r-- | z80_private.h | 5 |
5 files changed, 142 insertions, 23 deletions
@@ -46,12 +46,6 @@ static const char id[]="$Id$"; #define LO(w) ((w)&0xff) #define MK(h,l) (((Z80Word)(h))<<8|(l)) -typedef struct -{ - Z80Word address; - const char *label; -} Z80Label; - /* ---------------------------------------- GLOBALS */ static Z80 *z80; @@ -665,6 +659,8 @@ static void DoLabel(int no, const char *arg[]) if (label) label[f].label=NULL; + Z80SetLabels(label); + fclose(fp); } @@ -38,6 +38,7 @@ static const char ident[]="$Id$"; static const char ident_z80_header[]=Z80_H; static const char ident_z80_private_header[]=Z80_PRIVATE_H; +Z80Label *z80_labels=NULL; /* ---------------------------------------- PRIVATE FUNCTIONS */ @@ -309,6 +310,12 @@ Z80Val Z80Cycles(Z80 *cpu) } +void Z80SetLabels(Z80Label labels[]) +{ + z80_labels=labels; +} + + const char *Z80Disassemble(Z80 *cpu, Z80Word *pc) { #ifdef ENABLE_DISASSEM @@ -146,6 +146,17 @@ typedef enum } Z80FlagRegister; +/* Disassembly label -- only useful if ENABLE_DISASSEMBLER is set. + Labels are stored as an array, where a NULL in the label field marks + the end of the list. +*/ +typedef struct +{ + Z80Word address; + const char *label; +} Z80Label; + + /* ---------------------------------------- INTERFACES */ @@ -214,6 +225,11 @@ void Z80GetState(Z80 *cpu, Z80State *state); void Z80SetState(Z80 *cpu, const Z80State *state); +/* Set address to label mappings for the disassembler +*/ +void Z80SetLabels(Z80Label labels[]); + + /* Simple disassembly. addr is updated on exit. */ const char *Z80Disassemble(Z80 *cpu, Z80Word *addr); @@ -34,7 +34,6 @@ static const char ident[]="$Id$"; #include "z80.h" #include "z80_private.h" - static Z80Relative cb_off; /* ---------------------------------------- SHARED ROUTINES @@ -97,6 +96,25 @@ const char *Z80_Dis_GetArg(void) } +static const char *GetLabel(Z80Word w) +{ + if (z80_labels) + { + int f; + + for(f=0;z80_labels[f].label;f++) + { + if (z80_labels[f].address==w) + { + return z80_labels[f].label; + } + } + } + + return NULL; +} + + /* ---------------------------------------- CB xx BYTE OPCODES */ @@ -286,10 +304,14 @@ static void DIS_LD_IX_WORD(Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_LD_ADDR_IX(Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("ld",Z80_Dis_Printf("($%.4x),ix",w)); + if ((p=GetLabel(w))) + Z80_Dis_Set("ld",Z80_Dis_Printf("(%s),ix",p)); + else + Z80_Dis_Set("ld",Z80_Dis_Printf("($%.4x),ix",w)); } static void DIS_INC_IX(Z80 *z80, Z80Byte op, Z80Word *pc) @@ -320,9 +342,14 @@ static void DIS_ADD_IX_IX(Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_LD_IX_ADDR(Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("ld",Z80_Dis_Printf("ix,($%.4x)",w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("ld",Z80_Dis_Printf("ix,(%s)",p)); + else + Z80_Dis_Set("ld",Z80_Dis_Printf("ix,($%.4x)",w)); } static void DIS_DEC_IX(Z80 *z80, Z80Byte op, Z80Word *pc) @@ -704,9 +731,14 @@ static void DIS_SBC_HL_R16(Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_ED_LD_ADDR_R16(Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("ld",Z80_Dis_Printf("($%.4x),%s",w,z80_dis_reg16[(op-0x40)/16])); + + if ((p=GetLabel(w))) + Z80_Dis_Set("ld",Z80_Dis_Printf("(%s),%s",p,z80_dis_reg16[(op-0x40)/16])); + else + Z80_Dis_Set("ld",Z80_Dis_Printf("($%.4x),%s",w,z80_dis_reg16[(op-0x40)/16])); } static void DIS_NEG(Z80 *z80, Z80Byte op, Z80Word *pc) @@ -737,9 +769,14 @@ static void DIS_ADC_HL_R16(Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_ED_LD_R16_ADDR(Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("ld",Z80_Dis_Printf("%s,($%.4x)",z80_dis_reg16[(op-0x40)/16],w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("ld",Z80_Dis_Printf("%s,(%s)",z80_dis_reg16[(op-0x40)/16],p)); + else + Z80_Dis_Set("ld",Z80_Dis_Printf("%s,($%.4x)",z80_dis_reg16[(op-0x40)/16],w)); } static void DIS_RETI(Z80 *z80, Z80Byte op, Z80Word *pc) @@ -955,9 +992,14 @@ static void DIS_LD_IY_WORD(Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_LD_ADDR_IY(Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("ld",Z80_Dis_Printf("($%.4x),iy",w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("ld",Z80_Dis_Printf("(%s),iy",p)); + else + Z80_Dis_Set("ld",Z80_Dis_Printf("($%.4x),iy",w)); } static void DIS_INC_IY(Z80 *z80, Z80Byte op, Z80Word *pc) @@ -988,9 +1030,14 @@ static void DIS_ADD_IY_IY(Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_LD_IY_ADDR(Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("ld",Z80_Dis_Printf("iy,($%.4x)",w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("ld",Z80_Dis_Printf("iy,(%s)",p)); + else + Z80_Dis_Set("ld",Z80_Dis_Printf("iy,($%.4x)",w)); } static void DIS_DEC_IY(Z80 *z80, Z80Byte op, Z80Word *pc) @@ -1420,11 +1467,15 @@ static void DIS_RLA (Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_JR (Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word new; + const char *p; new=*pc+(Z80Relative)z80->memory[*pc]+1; (*pc)++; - Z80_Dis_Set("jr",Z80_Dis_Printf("$%.4x",new)); + if ((p=GetLabel(new))) + Z80_Dis_Set("jr",Z80_Dis_Printf("%s",p)); + else + Z80_Dis_Set("jr",Z80_Dis_Printf("$%.4x",new)); } static void DIS_RRA (Z80 *z80, Z80Byte op, Z80Word *pc) @@ -1436,20 +1487,29 @@ static void DIS_JR_CO (Z80 *z80, Z80Byte op, Z80Word *pc) { const char *con; Z80Word new; + const char *p; con=z80_dis_condition[(op-0x20)/8]; new=*pc+(Z80Relative)z80->memory[*pc]+1; (*pc)++; - Z80_Dis_Set("jr",Z80_Dis_Printf("%s,$%.4x",con,new)); + if ((p=GetLabel(new))) + Z80_Dis_Set("jr",Z80_Dis_Printf("%s,%s",con,p)); + else + Z80_Dis_Set("jr",Z80_Dis_Printf("%s,$%.4x",con,new)); } static void DIS_LD_ADDR_HL (Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("ld",Z80_Dis_Printf("($%.4x),hl",w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("ld",Z80_Dis_Printf("(%s),hl",p)); + else + Z80_Dis_Set("ld",Z80_Dis_Printf("($%.4x),hl",w)); } static void DIS_DAA (Z80 *z80, Z80Byte op, Z80Word *pc) @@ -1460,9 +1520,14 @@ static void DIS_DAA (Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_LD_HL_ADDR (Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("ld",Z80_Dis_Printf("hl,($%.4x)",w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("ld",Z80_Dis_Printf("hl,(%s)",p)); + else + Z80_Dis_Set("ld",Z80_Dis_Printf("hl,($%.4x)",w)); } static void DIS_CPL (Z80 *z80, Z80Byte op, Z80Word *pc) @@ -1473,9 +1538,14 @@ static void DIS_CPL (Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_LD_ADDR_A (Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("ld",Z80_Dis_Printf("($%.4x),a",w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("ld",Z80_Dis_Printf("(%s),a",p)); + else + Z80_Dis_Set("ld",Z80_Dis_Printf("($%.4x),a",w)); } static void DIS_SCF (Z80 *z80, Z80Byte op, Z80Word *pc) @@ -1486,9 +1556,14 @@ static void DIS_SCF (Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_LD_A_ADDR (Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("ld",Z80_Dis_Printf("a,($%.4x)",w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("ld",Z80_Dis_Printf("a,(%s)",p)); + else + Z80_Dis_Set("ld",Z80_Dis_Printf("a,($%.4x)",w)); } static void DIS_CCF (Z80 *z80, Z80Byte op, Z80Word *pc) @@ -1598,9 +1673,14 @@ static void DIS_POP_R16 (Z80 *z80, Z80Byte op, Z80Word *pc) static void DIS_JP (Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("jp",Z80_Dis_Printf("$%.4x",w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("jp",Z80_Dis_Printf("%s",p)); + else + Z80_Dis_Set("jp",Z80_Dis_Printf("$%.4x",w)); } static void DIS_PUSH_R16 (Z80 *z80, Z80Byte op, Z80Word *pc) @@ -1637,10 +1717,15 @@ static void DIS_JP_CO (Z80 *z80, Z80Byte op, Z80Word *pc) { const char *con; Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); con=z80_dis_condition[(op-0xc0)/8]; - Z80_Dis_Set("jp",Z80_Dis_Printf("%s,$%.4x",con,w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("jp",Z80_Dis_Printf("%s,%s",con,p)); + else + Z80_Dis_Set("jp",Z80_Dis_Printf("%s,$%.4x",con,w)); } static void DIS_CB_DECODE (Z80 *z80, Z80Byte op, Z80Word *pc) @@ -1655,18 +1740,28 @@ static void DIS_CALL_CO (Z80 *z80, Z80Byte op, Z80Word *pc) { const char *con; Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); con=z80_dis_condition[(op-0xc0)/8]; - Z80_Dis_Set("call",Z80_Dis_Printf("%s,$%.4x",con,w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("call",Z80_Dis_Printf("%s,%s",con,p)); + else + Z80_Dis_Set("call",Z80_Dis_Printf("%s,$%.4x",con,w)); } static void DIS_CALL (Z80 *z80, Z80Byte op, Z80Word *pc) { Z80Word w; + const char *p; w=Z80_Dis_FetchWord(z80,pc); - Z80_Dis_Set("call",Z80_Dis_Printf("$%.4x",w)); + + if ((p=GetLabel(w))) + Z80_Dis_Set("call",Z80_Dis_Printf("%s",p)); + else + Z80_Dis_Set("call",Z80_Dis_Printf("$%.4x",w)); } static void DIS_ADC_A_BYTE (Z80 *z80, Z80Byte op, Z80Word *pc) diff --git a/z80_private.h b/z80_private.h index 45e63bd..c10b15f 100644 --- a/z80_private.h +++ b/z80_private.h @@ -216,6 +216,11 @@ struct Z80 +/* ---------------------------------------- LABELS +*/ +extern Z80Label *z80_labels; + + /* ---------------------------------------- FLAG TABLES */ extern Z80Byte PSZtable[512]; |