dsforth: moved 8x8 printer to separate include (and made it configurable); added...
[urasm.git] / src / urdis.c
blob666a4239a0614ce3cff05e5dc7e82d544194f1af
1 // URASM Z80 assembler
2 // coded by Ketmar // Invisible Vector
3 // GPLv3 or later
4 //
5 //#define DUMP_IDX
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <unistd.h>
13 #include "liburasm/liburasm.h"
16 static uint8_t memory[65536];
19 static uint8_t getByte (uint16_t addr) {
20 return memory[addr];
24 static const char *cmdName (const char *s) {
25 static char cmd[16];
26 const char *t = strchr(s, '\t');
27 if (!t) return s;
28 memset(cmd, 0, sizeof(cmd));
29 memmove(cmd, s, t-s);
30 while (strlen(cmd) < 5+(urasm_allow_zxnext ? 3 : 0)) strcat(cmd, " ");
31 return cmd;
35 static const char *cmdArgs (const char *s) {
36 const char *t = strchr(s, '\t');
37 if (!t) return s+strlen(s);
38 return t+1;
42 int main (int argc, char *argv[]) {
43 char dstr[256];
44 uint16_t pc = 0x0100U;//0xECC8UL;
45 int maxlen = 1416;
46 int len, idx, len1, f;
47 for (int aidx = 1; aidx < argc; ++aidx) {
48 if (strcmp(argv[aidx], "--") == 0) {
49 for (int c = aidx+1; c < argc; ++c) argv[c-1] = argv[c];
50 --argc;
51 break;
53 if (strcmp(argv[aidx], "--z80") == 0 || strcmp(argv[aidx], "--z80a") == 0) {
54 for (int c = aidx+1; c < argc; ++c) argv[c-1] = argv[c];
55 --argc;
56 urasm_allow_zxnext = 0;
57 --aidx;
58 continue;
60 if (strcmp(argv[aidx], "--z80n") == 0 || strcmp(argv[aidx], "--z80next") == 0 ||
61 strcmp(argv[aidx], "--zxnext") == 0)
63 for (int c = aidx+1; c < argc; ++c) argv[c-1] = argv[c];
64 --argc;
65 urasm_allow_zxnext = 1;
66 --aidx;
67 continue;
70 FILE *fl = fopen(argc>1?argv[1]:"zout_0100.bin", "rb");
71 if (!fl) { fprintf(stderr, "ERROR: can't open input file!\n"); return 1; }
72 if (argc > 2) pc = (strtol(argv[2], NULL, 0))&0xffff;
73 maxlen = fread(memory+pc, 1, 65536-pc, fl);
74 fclose(fl);
76 urasm_getbyte = getByte;
77 while (maxlen > 0) {
78 if (pc >= 65535-16) break;
79 idx = urasm_disasm_opfind(pc);
80 //if (idx < 0) break;
81 len = urasm_disasm_oplen(idx);
82 len1 = urasm_disasm_opdisasm(dstr, pc);
83 printf("%04X: [", pc);
84 for (f = 0; f < len1; ++f) { if (f) putchar(' '); printf("%02X", memory[pc+f]); }
85 for (; f < 4; ++f) { if (f) putchar(' '); printf(" "); }
86 #ifdef DUMP_IDX
87 printf("] idx=%3d; len=%d; len1=%d; [%s%s]\n", idx, len, len1, cmdName(dstr), cmdArgs(dstr));
88 #else
89 printf("] len=%d; len1=%d; [%s%s]\n", len, len1, cmdName(dstr), cmdArgs(dstr));
90 #endif
91 if (len != len1) abort();
92 if (len <= 0) abort();
93 pc += len1; maxlen -= len1;
96 return 0;