X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=ao-tools%2Faltosui%2FAltosDebug.java;h=ca2e5a9046625465130c0f4dd1ec55f9c10cba48;hp=7bd3a5cd14a60fd40696cd7f347b00fbd8c12541;hb=f0fd423d0bf83bc5c3f9d39e9c09397fbe8caed2;hpb=ebeb13688a9a5442c838641ede6ba0dc92c9a1a4 diff --git a/ao-tools/altosui/AltosDebug.java b/ao-tools/altosui/AltosDebug.java index 7bd3a5cd..ca2e5a90 100644 --- a/ao-tools/altosui/AltosDebug.java +++ b/ao-tools/altosui/AltosDebug.java @@ -27,89 +27,73 @@ import altosui.AltosRomconfig; public class AltosDebug extends AltosSerial { - static final byte WR_CONFIG = 0x1d; - static final byte RD_CONFIG = 0x24; - static final byte CONFIG_TIMERS_OFF = (1 << 3); - static final byte CONFIG_DMA_PAUSE = (1 << 2); - static final byte CONFIG_TIMER_SUSPEND = (1 << 1); - static final byte SET_FLASH_INFO_PAGE = (1 << 0); - - static final byte GET_PC = 0x28; - static final byte READ_STATUS = 0x34; - static final byte STATUS_CHIP_ERASE_DONE = (byte) (1 << 7); - static final byte STATUS_PCON_IDLE = (1 << 6); - static final byte STATUS_CPU_HALTED = (1 << 5); - static final byte STATUS_POWER_MODE_0 = (1 << 4); - static final byte STATUS_HALT_STATUS = (1 << 3); - static final byte STATUS_DEBUG_LOCKED = (1 << 2); - static final byte STATUS_OSCILLATOR_STABLE = (1 << 1); - static final byte STATUS_STACK_OVERFLOW = (1 << 0); - - static final byte SET_HW_BRKPNT = 0x3b; - static byte HW_BRKPNT_N(byte n) { return (byte) ((n) << 3); } - static final byte HW_BRKPNT_N_MASK = (0x3 << 3); - static final byte HW_BRKPNT_ENABLE = (1 << 2); - - static final byte HALT = 0x44; - static final byte RESUME = 0x4c; - static byte DEBUG_INSTR(byte n) { return (byte) (0x54|(n)); } - static final byte STEP_INSTR = 0x5c; - static byte STEP_REPLACE(byte n) { return (byte) (0x64|(n)); } - static final byte GET_CHIP_ID = 0x68; - - - static boolean ishex(int c) { - if ('0' <= c && c <= '9') - return true; - if ('a' <= c && c <= 'f') - return true; - if ('A' <= c && c <= 'F') - return true; - return false; - } - - static boolean ishex(String s) { - for (int i = 0; i < s.length(); i++) - if (!ishex(s.charAt(i))) - return false; - return true; - } - static boolean isspace(int c) { - switch (c) { - case ' ': - case '\t': - return true; - } - return false; - } + public static final byte WR_CONFIG = 0x1d; + public static final byte RD_CONFIG = 0x24; + public static final byte CONFIG_TIMERS_OFF = (1 << 3); + public static final byte CONFIG_DMA_PAUSE = (1 << 2); + public static final byte CONFIG_TIMER_SUSPEND = (1 << 1); + public static final byte SET_FLASH_INFO_PAGE = (1 << 0); + + public static final byte GET_PC = 0x28; + public static final byte READ_STATUS = 0x34; + public static final byte STATUS_CHIP_ERASE_DONE = (byte) (1 << 7); + public static final byte STATUS_PCON_IDLE = (1 << 6); + public static final byte STATUS_CPU_HALTED = (1 << 5); + public static final byte STATUS_POWER_MODE_0 = (1 << 4); + public static final byte STATUS_HALT_STATUS = (1 << 3); + public static final byte STATUS_DEBUG_LOCKED = (1 << 2); + public static final byte STATUS_OSCILLATOR_STABLE = (1 << 1); + public static final byte STATUS_STACK_OVERFLOW = (1 << 0); + + public static final byte SET_HW_BRKPNT = 0x3b; + public static byte HW_BRKPNT_N(byte n) { return (byte) ((n) << 3); } + public static final byte HW_BRKPNT_N_MASK = (0x3 << 3); + public static final byte HW_BRKPNT_ENABLE = (1 << 2); + + public static final byte HALT = 0x44; + public static final byte RESUME = 0x4c; + public static byte DEBUG_INSTR(byte n) { return (byte) (0x54|(n)); } + public static final byte STEP_INSTR = 0x5c; + public static byte STEP_REPLACE(byte n) { return (byte) (0x64|(n)); } + public static final byte GET_CHIP_ID = 0x68; - static int fromhex(int c) { - if ('0' <= c && c <= '9') - return c - '0'; - if ('a' <= c && c <= 'f') - return c - 'a' + 10; - if ('A' <= c && c <= 'F') - return c - 'A' + 10; - return -1; - } boolean debug_mode; void ensure_debug_mode() { if (!debug_mode) { - printf("D\n"); + printf("m 0\nD\n"); + flush_reply(); debug_mode = true; } } + void dump_memory(String header, int address, byte[] bytes, int start, int len) { + System.out.printf("%s\n", header); + for (int j = 0; j < len; j++) { + if ((j & 15) == 0) { + if (j != 0) + System.out.printf("\n"); + System.out.printf ("%04x:", address + j); + } + System.out.printf(" %02x", bytes[start + j]); + } + System.out.printf("\n"); + } + /* * Write target memory */ - public void write_memory(int address, byte[] bytes) { + public void write_memory(int address, byte[] bytes, int start, int len) { ensure_debug_mode(); - printf("O %x %x\n", bytes.length, address); - for (int i = 0; i < bytes.length; i++) - printf("%02x", bytes[i]); +// dump_memory("write_memory", address, bytes, start, len); + printf("O %x %x\n", len, address); + for (int i = 0; i < len; i++) + printf("%02x", bytes[start + i]); + } + + public void write_memory(int address, byte[] bytes) { + write_memory(address, bytes, 0, bytes.length); } /* @@ -123,18 +107,21 @@ public class AltosDebug extends AltosSerial { ensure_debug_mode(); printf("I %x %x\n", length, address); int i = 0; + int start = 0; while (i < length) { String line = get_reply().trim(); - if (!ishex(line) || line.length() % 2 != 0) + if (!Altos.ishex(line) || line.length() % 2 != 0) throw new IOException( String.format ("Invalid reply \"%s\"", line)); int this_time = line.length() / 2; for (int j = 0; j < this_time; j++) - data[j] = (byte) ((fromhex(line.charAt(j*2)) << 4) + - fromhex(line.charAt(j*2+1))); + data[start + j] = (byte) ((Altos.fromhex(line.charAt(j*2)) << 4) + + Altos.fromhex(line.charAt(j*2+1))); + start += this_time; i += this_time; } +// dump_memory("read_memory", address, data, 0, length); return data; } @@ -177,7 +164,7 @@ public class AltosDebug extends AltosSerial { String line = get_reply().trim(); String tokens[] = line.split("\\s+"); for (int j = 0; j < tokens.length; j++) { - if (!ishex(tokens[j]) || + if (!Altos.ishex(tokens[j]) || tokens[j].length() != 2) throw new IOException( String.format @@ -195,14 +182,60 @@ public class AltosDebug extends AltosSerial { return data; } + public byte read_byte() throws IOException, InterruptedException { + return read_bytes(1)[0]; + } + + public byte debug_instr(byte[] instruction) throws IOException, InterruptedException { + byte[] command = new byte[1 + instruction.length]; + command[0] = DEBUG_INSTR((byte) instruction.length); + for (int i = 0; i < instruction.length; i++) + command[i+1] = instruction[i]; + write_bytes(command); + return read_byte(); + } + + public byte resume() throws IOException, InterruptedException { + write_byte(RESUME); + return read_byte(); + } + + public int read_uint16() throws IOException, InterruptedException { + byte[] d = read_bytes(2); + return ((int) (d[0] & 0xff) << 8) | (d[1] & 0xff); + } + + public int read_uint8() throws IOException, InterruptedException { + byte[] d = read_bytes(1); + return (int) (d[0] & 0xff); + } + + public int get_chip_id() throws IOException, InterruptedException { + write_byte(GET_CHIP_ID); + return read_uint16(); + } + + public int get_pc() throws IOException, InterruptedException { + write_byte(GET_PC); + return read_uint16(); + } + public byte read_status() throws IOException, InterruptedException { write_byte(READ_STATUS); - return read_bytes(2)[0]; + return read_byte(); + } + + static final byte LJMP = 0x02; + + public void set_pc(int pc) throws IOException, InterruptedException { + byte high = (byte) (pc >> 8); + byte low = (byte) pc; + byte[] jump_mem = { LJMP, high, low }; + debug_instr(jump_mem); } public boolean check_connection() throws IOException, InterruptedException { byte reply = read_status(); - System.out.printf("status %x\n", reply); if ((reply & STATUS_CHIP_ERASE_DONE) == 0) return false; if ((reply & STATUS_PCON_IDLE) != 0)