git.haldean.org x6502 / 7c37fb1
Some tidying, starting to work on a cooler demo. Will Haldean Brown 7 years ago
7 changed file(s) with 120 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
1313 #define FLAG_ZERO 0x02
1414 #define FLAG_CARRY 0x01
1515
16 // set if memory was modified during processing of the last instruction
1617 #define EMU_FLAG_DIRTY 0x01
18 // set if the emulator should wait for an interrupt before continuing
19 #define EMU_FLAG_WAIT_FOR_INTERRUPT 0x02
1720
1821 typedef struct {
1922 // program counter
88 init_names();
99
1010 int i;
11 printf("pc %04X\nx %02X y %02X sp %02X sr %02X ac %02X",
11 fprintf(stderr, "pc %04X\nx %02X y %02X sp %02X sr %02X ac %02X",
1212 m->pc, m->x, m->y, m->sp, m->sr, m->ac);
1313
14 printf("\nlast opcode: %s (%02X)",
14 fprintf(stderr, "\nlast opcode: %s (%02X)",
1515 inst_names[m->last_opcode], m->last_opcode);
16 printf(", next opcode: %s (%02X)",
16 fprintf(stderr, ", next opcode: %s (%02X)",
1717 inst_names[m->mem[m->pc]], m->mem[m->pc]);
1818
19 printf("\nflags n %d o %d b %d d %d i %d z %d c %d",
19 fprintf(stderr, "\nflags n %d o %d b %d d %d i %d z %d c %d",
2020 (m->sr & FLAG_NEGATIVE) > 0,
2121 (m->sr & FLAG_OVERFLOW) > 0,
2222 (m->sr & FLAG_BREAK) > 0,
2525 (m->sr & FLAG_ZERO) > 0,
2626 (m->sr & FLAG_CARRY) > 0);
2727
28 printf("\nmem ");
28 fprintf(stderr, "\nmem ");
2929
3030 int mem_offset = m->pc - MEM_PRINT_BYTES / 2;
3131 // clamp to [0, MAX_MEM_OFFSET]
3333 mem_offset > MAX_MEM_OFFSET ? MAX_MEM_OFFSET : mem_offset);
3434
3535 for (i = 0; i < MEM_PRINT_BYTES; i++) {
36 printf("%02X ", m->mem[i + mem_offset]);
36 fprintf(stderr, "%02X ", m->mem[i + mem_offset]);
3737 }
3838
39 printf("\n ");
39 fprintf(stderr, "\n ");
4040 for (i = 0; i < m->pc - mem_offset; i++) {
41 printf(" ");
41 fprintf(stderr, " ");
4242 }
43 printf("^^ (%04x)", m->pc);
43 fprintf(stderr, "^^ (%04x)", m->pc);
4444
45 printf("\nstack ");
45 fprintf(stderr, "\nstack ");
4646 for (i = 0; i < MEM_PRINT_BYTES; i++) {
47 printf("%02X ", m->mem[STACK_START + 0xFF - i]);
47 fprintf(stderr, "%02X ", m->mem[STACK_START + 0xFF - i]);
4848 }
4949 int off = 0xFF - m->sp;
5050 if (off < MEM_PRINT_BYTES) {
51 printf("\n ");
51 fprintf(stderr, "\n ");
5252 for (i = 0; i < off; i++) {
53 printf(" ");
53 fprintf(stderr, " ");
5454 }
55 printf("^^");
55 fprintf(stderr, "^^");
5656 }
5757
58 printf("\n\n");
58 fprintf(stderr, "\n\n");
5959 }
2424 // branch_offset is an offset that will be added to the program counter
2525 // after we move to the next instruction
2626 int8_t branch_offset = 0;
27
28 // if set to true, the next instruction will not be executed until an
29 // interrupt is fired from the IO bus and handled.
30 uint8_t wait_for_interrupt = 0;
3127
3228 init_io();
3329
8076
8177 do {
8278 handle_io(m);
83 } while (wait_for_interrupt && !m->interrupt_waiting);
84 wait_for_interrupt = 0;
79 // clear dirty memory flag immediately so that subsequent runs don't
80 // redo whatever I/O operation is associated with the dirty memaddr
81 m->emu_flags &= ~EMU_FLAG_DIRTY;
82 } while ((m->emu_flags & EMU_FLAG_WAIT_FOR_INTERRUPT) &&
83 !m->interrupt_waiting);
8584
8685 if (m->interrupt_waiting && !get_flag(m, FLAG_INTERRUPT)) {
8786 STACK_PUSH(m) = (m->pc & 0xFF00) >> 8;
2424 noecho();
2525 nodelay(stdscr, TRUE);
2626 keypad(stdscr, TRUE);
27 curs_set(0);
2728
2829 io_supports_paint = (has_colors() != FALSE);
2930 if (io_supports_paint) {
4950 void init_vterm() {
5051 window = newwin(VTERM_ROWS + 2, VTERM_COLS + 2, 0, 0);
5152 box(window, 0, 0);
53 wrefresh(window);
5254 }
5355
5456 void finish_vterm() {
5860 void update_modeflags(uint8_t old_flags, uint8_t new_flags) {
5961 io_modeflags = new_flags;
6062
63 // if the vterm flag changed (avoids reinit every time flags change)
6164 if ((new_flags ^ old_flags) & IO_MODEFLAG_VTERM) {
6265 if (new_flags & IO_MODEFLAG_VTERM) {
6366 init_vterm();
99 break;
1010
1111 case WAI:
12 wait_for_interrupt = 1;
12 m->emu_flags |= EMU_FLAG_WAIT_FOR_INTERRUPT;
1313 break;
0 #include <stdio.s>
1
2 rows = 25
3 cols = 40
4
5 ; load location of ISR
6 lda #<isr
7 sta $FFFE
8 lda #>isr
9 sta $FFFF
10
11 ; activate vterm mode
12 lda #$01
13 sta $FF02
14
15 ; shiny pretty colors
16 lda #$04
17 sta paint
18
19 ; register and addressing init
20 lda #$00
21 sta $00
22 lda #$FB
23 sta $01
24
25 lda #$A4
26 ldy #$00
27
28 cli
29
30 loop
31 sta ($00),Y
32 wai
33 jmp loop
34
35 isr:
36 pha
37
38 ; clear existing character
39 lda #$20
40 sta ($00),Y
41
42 lda getc
43
44 cmp #$71 ; 71 == 'q'
45 bne checkj
46 ext
47
48 checkj:
49 cmp #$6A ; 6A == 'j'
50 bne checkk
51 ; it's a j. move down.
52 tya
53 clc
54 adc #cols
55 jmp checkbounds
56
57 checkk:
58 cmp #$6B ; 6B == 'k'
59 bne checkh
60 ; it's a k. move up.
61 tya
62 sec
63 sbc #cols
64 jmp checkbounds
65
66 checkh:
67 cmp #$68 ; 68 == 'h'
68 bne checkl
69 ; it's an h. move left.
70 tya
71 sec
72 sbc #$02
73 jmp checkbounds
74
75 checkl:
76 cmp #$6C ; 6C == 'l'
77 bne done
78 ; it's an h. move left.
79 tya
80 clc
81 adc #$02
82 jmp checkbounds
83
84 checkbounds:
85 ; TODO
86 tay
87
88 done:
89 pla
90 rti
11
22 putc = $FF00
33 getc = $FF01
4 iomode = $FF02
5 paint = $FEE8
46
57 #define debug .byt $FC
68 #define ext .byt $FF