swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 1 | #include "common.h" |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 2 | #include "cpu.h" |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 3 | #include "dbg.h" |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 4 | #include "gui.h" |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 5 | #include "screen.h" |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 6 | |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 7 | #include <bits/getopt_core.h> |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 8 | #include <ctype.h> |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 9 | #include <errno.h> |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <unistd.h> |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 13 | #include <pthread.h> |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 14 | #include <mqueue.h> |
| 15 | #include <sys/stat.h> |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 16 | |
| 17 | |
| 18 | void cleanup_screen_thread(pthread_t thread) |
| 19 | { |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 20 | puts("Cleaning up screen..."); |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 21 | pthread_cancel(thread); |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 22 | } |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 23 | |
| 24 | void cleanup_debug_prompt_thread(pthread_t thread) |
| 25 | { |
| 26 | puts("Cleaning up debug prompt..."); |
| 27 | pthread_cancel(thread); |
| 28 | } |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 29 | |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 30 | int main(int argc, char **argv) |
| 31 | { |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 32 | bool disflag = false, |
| 33 | runflag = false, |
| 34 | helpflag = false, |
| 35 | debugflag = false, |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 36 | should_read = false, |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 37 | guiflag = false, |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 38 | scrflag = false, |
| 39 | nohaltflag = false; |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 40 | |
| 41 | int disasm_len = 0; |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 42 | |
| 43 | FILE *input = stdin; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 44 | |
| 45 | char c; |
| 46 | |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 47 | while ((c = getopt(argc, argv, "HDsdrhgi:n:")) != -1) |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 48 | { |
| 49 | switch (c) |
| 50 | { |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 51 | case 'H': |
| 52 | nohaltflag = true; |
| 53 | break; |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 54 | case 'd': |
| 55 | disflag = true; |
| 56 | should_read = true; |
| 57 | break; |
| 58 | case 'r': |
| 59 | runflag = true; |
| 60 | should_read = true; |
| 61 | break; |
| 62 | case 'D': |
| 63 | debugflag = true; |
| 64 | should_read = true; |
| 65 | break; |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 66 | case 'g': |
| 67 | guiflag = true; |
| 68 | should_read = true; |
| 69 | break; |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 70 | case 'i': |
| 71 | input = fopen(optarg, "r"); |
| 72 | break; |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 73 | case 'n': |
| 74 | disasm_len = atoi(optarg); |
| 75 | break; |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 76 | case 's': |
| 77 | scrflag = true; |
| 78 | break; |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 79 | case 'h': |
| 80 | case '?': |
| 81 | helpflag = 1; |
| 82 | break; |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | if (helpflag) |
| 87 | { |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 88 | printf("6502 emulator, disassembler and debugger\n" |
| 89 | "Usage:\n" |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 90 | " -g use GUI\n" |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 91 | " -s use SDL screen (faster than GUI debugger)\n" |
| 92 | " -H keep running after CPU halts (useful on windows and to look at screen)\n" |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 93 | " -d disassemble input\n" |
| 94 | " -r run input\n" |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 95 | " -D open CLI debug prompt (like gdb)\n" |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 96 | " -i <input> set input file, defaults to standard input\n" |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 97 | " -n <number> number of instructions to disassemble, 0 for all\n" |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 98 | " -h, -? show this help page\n"); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 102 | cpu_t cpu; |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 103 | mqd_t mq_to_cpu; |
| 104 | |
| 105 | struct mq_attr attrs; |
| 106 | attrs.mq_maxmsg = 10; |
| 107 | attrs.mq_msgsize = MQ_BUF_LEN; |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 108 | |
| 109 | if (should_read) |
| 110 | { |
| 111 | cpu = new_cpu(); |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 112 | fread(cpu.mem + 0x600, 0xFFFF - 0x600, 1, input); |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 113 | |
| 114 | int unlink = mq_unlink(MQ_NAME); |
| 115 | if (unlink < 0 && errno != ENOENT) |
| 116 | { |
| 117 | printf("Warning: mq_unlink() error: %d %s\n", errno, strerror(errno)); |
| 118 | } |
| 119 | |
| 120 | mq_to_cpu = mq_open(MQ_NAME, O_RDWR | O_CREAT | O_NONBLOCK, S_IWUSR|S_IRUSR, &attrs); |
| 121 | printf("error after mq_open (%ld) = %d %s\n", attrs.mq_msgsize, errno, strerror(errno)); |
| 122 | ASSERT("Open message queue for emulator", mq_to_cpu > 0) |
| 123 | |
| 124 | mq_send(mq_to_cpu, "init", 5, 2); |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 125 | } |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 126 | else |
| 127 | { |
| 128 | puts("6502 toolchain by swissChili <swisschili.sh>"); |
| 129 | printf("%s -h for help\n", argv[0]); |
| 130 | } |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 131 | |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 132 | if (scrflag) |
| 133 | { |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 134 | CATCH(&cleanup_screen_thread, start_screen_thread(cpu.mem + CPU_FB_ADDR)); |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 135 | } |
| 136 | |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 137 | if (guiflag && scrflag) |
| 138 | { |
| 139 | THROW("-g and -s cannot be used together"); |
| 140 | } |
| 141 | |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 142 | if (guiflag) |
| 143 | { |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 144 | start_gui(mq_to_cpu, &cpu); |
| 145 | run_mq(&cpu, mq_to_cpu); |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 146 | } |
| 147 | else if (disflag) |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 148 | { |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 149 | disas_num(&cpu, 64); |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 150 | } |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 151 | else if (runflag) |
| 152 | { |
| 153 | run(&cpu); |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 154 | if (nohaltflag) |
| 155 | { |
| 156 | puts("Press any key to exit"); |
| 157 | getchar(); |
| 158 | } |
swissChili | da4803e | 2020-08-06 20:06:04 -0700 | [diff] [blame] | 159 | } |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 160 | else if (debugflag) |
| 161 | { |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 162 | CATCH(&cleanup_debug_prompt_thread, start_debug_prompt(mq_to_cpu, &cpu)); |
| 163 | run_mq(&cpu, mq_to_cpu); |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 164 | } |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 165 | |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 166 | if (should_read) |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 167 | { |
swissChili | 62d6d5d | 2020-07-30 20:12:47 -0700 | [diff] [blame] | 168 | free_cpu(&cpu); |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 169 | mq_close(mq_to_cpu); |
| 170 | } |
swissChili | 6c61a79 | 2020-07-28 16:29:20 -0700 | [diff] [blame] | 171 | } |