annotate src/main.c @ 26:083347ccd508

Implemented vblank interrupts and fixed a bug in exception vector address calculation
author Michael Pavone <pavone@retrodev.com>
date Fri, 01 Apr 2016 21:34:38 -0700
parents fb14515266f4
children c677507682e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <stdint.h>
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include <stdio.h>
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include <stdlib.h>
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include <string.h>
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 #include "cpu.h"
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
6 #include "vdp.h"
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
7 #include "audio.h"
25
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
8 #include "timer.h"
11
04d8efe7a1f0 Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents: 8
diff changeset
9 #include "system.h"
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
11 #define CYCLES_PER_FRAME (832*262)
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
12 #define MASTER_CLOCK 13056000
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 uint8_t rom[48 * 1024];
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 uint8_t ram[16 * 1024];
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 enum {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 PORT_CONTROLLER_1,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 PORT_CONTROLLER_2,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 PORT_CONTROLLER_3,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 PORT_CONTROLLER_4,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 PORT_FREQUENCY_A,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 PORT_FREQUENCY_B,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 PORT_FREQUENCY_C,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 PORT_FREQUENCY_D,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 PORT_VOLUME_AB,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 PORT_VOLUME_CD,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 PORT_TIMER,
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 PORT_SERIAL,
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
30 PORT_VERTICAL,
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
31 PORT_HORIZONTAL,
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
32 PORT_VRAM_ADDRESS,
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
33 PORT_VRAM_DATA
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 };
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
36 typedef struct {
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
37 cpu *proc;
25
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
38 audio *audio;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
39 timer timer;
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
40 vdp video;
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
41 } console;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
42
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 void debug_port_write(cpu *context, uint8_t port, uint16_t value)
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 putchar(value);
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 }
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 uint16_t debug_port_read(cpu *context, uint8_t port)
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 return getchar();
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 }
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
53 void vertical_port_write(cpu *context, uint8_t port, uint16_t value)
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
54 {
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
55 console *system = context->system;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
56 vdp_run(&system->video, context->cycles);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
57 system->video.vscroll = value;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
58 }
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
59
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
60 uint16_t vertical_port_read(cpu *context, uint8_t port)
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
61 {
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
62 console *system = context->system;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
63 vdp_run(&system->video, context->cycles);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
64 return system->video.vcounter;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
65 }
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
66
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
67 void horizontal_port_write(cpu *context, uint8_t port, uint16_t value)
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
68 {
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
69 console *system = context->system;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
70 vdp_run(&system->video, context->cycles);
19
04fc17376999 Sort of working tile rendering and tile test ROM
Michael Pavone <pavone@retrodev.com>
parents: 16
diff changeset
71 vdp_write_hscroll(&system->video, value);
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
72 }
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
73
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
74 uint16_t horizontal_port_read(cpu *context, uint8_t port)
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
75 {
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
76 console *system = context->system;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
77 vdp_run(&system->video, context->cycles);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
78 return system->video.hcounter;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
79 }
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
80
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
81 void address_port_write(cpu *context, uint8_t port, uint16_t value)
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
82 {
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
83 console *system = context->system;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
84 vdp_run(&system->video, context->cycles);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
85 vdp_write_address(&system->video, value);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
86 }
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
87
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
88 uint16_t address_port_read(cpu *context, uint8_t port)
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
89 {
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
90 console *system = context->system;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
91 vdp_run(&system->video, context->cycles);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
92 return system->video.status;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
93 }
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
94
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
95 void data_port_write(cpu *context, uint8_t port, uint16_t value)
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
96 {
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
97 console *system = context->system;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
98 vdp_run(&system->video, context->cycles);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
99 vdp_write_data(&system->video, value);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
100 }
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
101
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
102 void frequency_port_write(cpu *context, uint8_t port, uint16_t value)
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
103 {
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
104 console *system = context->system;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
105 audio_run(system->audio, context->cycles);
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
106 audio_write_freq(system->audio, port - PORT_FREQUENCY_A, value);
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
107 }
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
108
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
109 void volume_port_write(cpu *context, uint8_t port, uint16_t value)
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
110 {
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
111 console *system = context->system;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
112 audio_run(system->audio, context->cycles);
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
113 audio_write_vol(system->audio, port - PORT_VOLUME_AB, value);
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
114 }
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
115
25
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
116 void timer_port_write(cpu *context, uint8_t port, uint16_t value)
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
117 {
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
118 console *system = context->system;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
119 timer_run(&system->timer, context->cycles);
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
120 timer_write(&system->timer, value);
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
121 uint32_t next_int = next_interrupt_cycle(context, (~context->pending_interrupts) & 3);
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
122 if (next_int < context->current_target) {
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
123 context->current_target = next_int;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
124 }
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
125 }
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
126
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
127 uint32_t next_interrupt_cycle(cpu *context, uint8_t mask)
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
128 {
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
129 console *system = context->system;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
130 uint32_t next = 0xFFFFFFFF;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
131 if (mask & 1) {
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
132 timer_run(&system->timer, context->cycles);
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
133 next = timer_next_interrupt(&system->timer);
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
134 }
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
135 if (mask & 2) {
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
136 vdp_run(&system->video, context->cycles);
26
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
137 uint32_t vnext = vdp_next_interrupt(&system->video);
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
138 if (vnext < next) {
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
139 next = vnext;
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
140 }
25
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
141 }
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
142 return next;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
143 }
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
144
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
145 uint8_t get_current_interrupts(cpu *context)
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
146 {
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
147 console *system = context->system;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
148 timer_run(&system->timer, context->cycles);
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
149 uint8_t bits = 0;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
150 if (system->timer.pending) {
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
151 bits |= 1;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
152 }
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
153 vdp_run(&system->video, context->cycles);
26
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
154 if (vdp_interrupt_pending(&system->video)) {
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
155 bits |= 2;
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
156 }
25
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
157 return bits;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
158 }
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
159
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
160 void ack_interrupt(cpu *context, int which)
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
161 {
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
162 console *system = context->system;
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
163 if (which == 0) {
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
164 timer_run(&system->timer, context->cycles);
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
165 system->timer.pending = 0;
26
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
166 } else {
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
167 vdp_ack_interrupt(&system->video);
25
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
168 }
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
169 }
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
170
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
171 memory_region regions[] = {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
172 {rom, 0, sizeof(rom)-1, MEM_READ},
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
173 {ram, sizeof(rom), sizeof(rom)-1+sizeof(ram), MEM_READ},
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
174 };
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
175
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
176 void run_console(console *context)
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
177 {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
178 for(;;)
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
179 {
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
180 run_cpu(context->proc, CYCLES_PER_FRAME);
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
181 audio_run(context->audio, CYCLES_PER_FRAME);
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
182 vdp_run(&context->video, CYCLES_PER_FRAME);
25
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
183 timer_run(&context->timer, CYCLES_PER_FRAME);
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
184 context->proc->cycles -= CYCLES_PER_FRAME;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
185 context->video.cycles -= CYCLES_PER_FRAME;
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
186 context->audio->cycles -= CYCLES_PER_FRAME;
25
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
187 context->timer.cycles -= CYCLES_PER_FRAME;
16
ae58e7c3c328 Poll events regularly to avoid unresponsive app warnings. Handle quit event
Michael Pavone <pavone@retrodev.com>
parents: 11
diff changeset
188 system_poll_events();
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
189 }
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
190 }
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
191
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
192 int main(int argc, char **argv)
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
193 {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
194 if (argc < 2) {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
195 fputs("usage: s16 FILE\n", stderr);
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
196 return 1;
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
197 }
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
198 FILE *f = fopen(argv[1], "rb");
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
199 if (!f) {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
200 fprintf(stderr, "Failed to open %s for reading\n", argv[1]);
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
201 return 1;
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
202 }
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
203
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
204 size_t read;
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
205 if ((read = fread(rom, 1, sizeof(rom), f)) < sizeof(rom)) {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
206 memset(rom + read, 0xFF, sizeof(rom)-read);
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
207 }
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
208 console context;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
209 context.proc = alloc_cpu(10, sizeof(regions)/sizeof(memory_region), regions);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
210 context.proc->system = &context;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
211 vdp_init(&context.video, 2);
25
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
212 timer_init(&context.timer, 16);
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
213 context.proc->port_handlers[PORT_FREQUENCY_A].write = frequency_port_write;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
214 context.proc->port_handlers[PORT_FREQUENCY_B].write = frequency_port_write;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
215 context.proc->port_handlers[PORT_FREQUENCY_C].write = frequency_port_write;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
216 context.proc->port_handlers[PORT_FREQUENCY_D].write = frequency_port_write;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
217 context.proc->port_handlers[PORT_VOLUME_AB].write = volume_port_write;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
218 context.proc->port_handlers[PORT_VOLUME_CD].write = volume_port_write;
25
fb14515266f4 Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents: 24
diff changeset
219 context.proc->port_handlers[PORT_TIMER].write = timer_port_write;
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
220 context.proc->port_handlers[PORT_SERIAL].write = debug_port_write;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
221 context.proc->port_handlers[PORT_SERIAL].read = debug_port_read;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
222 context.proc->port_handlers[PORT_VERTICAL].write = vertical_port_write;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
223 context.proc->port_handlers[PORT_VERTICAL].read = vertical_port_read;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
224 context.proc->port_handlers[PORT_HORIZONTAL].write = horizontal_port_write;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
225 context.proc->port_handlers[PORT_HORIZONTAL].read = horizontal_port_read;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
226 context.proc->port_handlers[PORT_VRAM_ADDRESS].write = address_port_write;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
227 context.proc->port_handlers[PORT_VRAM_ADDRESS].read = address_port_read;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
228 context.proc->port_handlers[PORT_VRAM_DATA].write = data_port_write;
11
04d8efe7a1f0 Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents: 8
diff changeset
229
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
230 if (!system_init(640, 480, 48000)) {
11
04d8efe7a1f0 Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents: 8
diff changeset
231 return 1;
04d8efe7a1f0 Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents: 8
diff changeset
232 }
04d8efe7a1f0 Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents: 8
diff changeset
233
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
234 context.audio = alloc_audio(MASTER_CLOCK, 17, system_sample_rate(), system_buffer_size());
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
235
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
236 run_console(&context);
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
237 return 0;
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
238 }