Mercurial > repos > simple16
annotate src/main.c @ 31:b87b3ad5068c
Forgot to add the controller source files
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 03 Apr 2016 18:37:31 -0700 |
parents | 78068060313a |
children | eda4919d955f |
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 | 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" |
28
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
9 #include "controller.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
|
10 #include "system.h" |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
12 #define CYCLES_PER_FRAME (832*262) |
24 | 13 #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
|
14 |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 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
|
16 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
|
17 |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 enum { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 PORT_CONTROLLER_1, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 PORT_CONTROLLER_2, |
28
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
21 RESERVED_1, |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
22 RESERVED_2, |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 PORT_FREQUENCY_A, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 PORT_FREQUENCY_B, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 PORT_FREQUENCY_C, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 PORT_FREQUENCY_D, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 PORT_VOLUME_AB, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 PORT_VOLUME_CD, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 PORT_TIMER, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 PORT_SERIAL, |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
31 PORT_VERTICAL, |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
32 PORT_HORIZONTAL, |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
33 PORT_VRAM_ADDRESS, |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
34 PORT_VRAM_DATA |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 }; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
37 typedef struct { |
28
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
38 cpu *proc; |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
39 audio *audio; |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
40 timer timer; |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
41 controllers pads; |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
42 vdp video; |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
43 } console; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
44 |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 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
|
46 { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 putchar(value); |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 } |
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 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
|
51 { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 return getchar(); |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 } |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
55 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
|
56 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
57 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
58 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
59 system->video.vscroll = value; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
60 } |
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 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
|
63 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
64 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
65 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
66 return system->video.vcounter; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
67 } |
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 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
|
70 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
71 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
72 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
|
73 vdp_write_hscroll(&system->video, value); |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
74 } |
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 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
|
77 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
78 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
79 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
80 return system->video.hcounter; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
81 } |
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 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
|
84 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
85 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
86 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
87 vdp_write_address(&system->video, value); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
88 } |
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 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
|
91 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
92 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
93 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
94 return system->video.status; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
95 } |
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 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
|
98 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
99 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
100 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
101 vdp_write_data(&system->video, value); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
102 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
103 |
24 | 104 void frequency_port_write(cpu *context, uint8_t port, uint16_t value) |
105 { | |
106 console *system = context->system; | |
107 audio_run(system->audio, context->cycles); | |
108 audio_write_freq(system->audio, port - PORT_FREQUENCY_A, value); | |
109 } | |
110 | |
111 void volume_port_write(cpu *context, uint8_t port, uint16_t value) | |
112 { | |
113 console *system = context->system; | |
114 audio_run(system->audio, context->cycles); | |
115 audio_write_vol(system->audio, port - PORT_VOLUME_AB, value); | |
116 } | |
117 | |
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
|
118 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
|
119 { |
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 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
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 } |
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 |
28
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
129 uint16_t controller_port_read(cpu *context, uint8_t port) |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
130 { |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
131 //process events so controller state is as fresh as possible |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
132 system_poll_events(); |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
133 console *system = context->system; |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
134 return controller_read(&system->pads, port - PORT_CONTROLLER_1); |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
135 } |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
136 |
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
|
137 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
|
138 { |
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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
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 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
|
146 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
|
147 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
|
148 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
|
149 next = vnext; |
083347ccd508
Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents:
25
diff
changeset
|
150 } |
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
|
151 } |
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 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
|
153 } |
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
|
154 |
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
|
155 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
|
156 { |
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 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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 } |
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 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
|
164 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
|
165 bits |= 2; |
083347ccd508
Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents:
25
diff
changeset
|
166 } |
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
|
167 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
|
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 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
|
171 { |
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
|
172 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
|
173 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
|
174 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
|
175 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
|
176 } else { |
083347ccd508
Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents:
25
diff
changeset
|
177 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
|
178 } |
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
|
179 } |
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
|
180 |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
181 memory_region regions[] = { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 {rom, 0, sizeof(rom)-1, MEM_READ}, |
30
78068060313a
Fixed memory flags on RAM
Michael Pavone <pavone@retrodev.com>
parents:
28
diff
changeset
|
183 {ram, sizeof(rom), sizeof(rom)-1+sizeof(ram), MEM_READ|MEM_WRITE}, |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 }; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
186 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
|
187 { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 for(;;) |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 { |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
190 run_cpu(context->proc, CYCLES_PER_FRAME); |
24 | 191 audio_run(context->audio, CYCLES_PER_FRAME); |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
192 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
|
193 timer_run(&context->timer, CYCLES_PER_FRAME); |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
194 context->proc->cycles -= CYCLES_PER_FRAME; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
195 context->video.cycles -= CYCLES_PER_FRAME; |
24 | 196 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
|
197 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
|
198 system_poll_events(); |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
199 } |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
200 } |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
201 |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
202 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
|
203 { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 if (argc < 2) { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
205 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
|
206 return 1; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
207 } |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
208 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
|
209 if (!f) { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
210 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
|
211 return 1; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
212 } |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
213 |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
214 size_t read; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
215 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
|
216 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
|
217 } |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
218 console context; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
219 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
|
220 context.proc->system = &context; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
221 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
|
222 timer_init(&context.timer, 16); |
28
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
223 context.proc->port_handlers[PORT_CONTROLLER_1].read = controller_port_read; |
c677507682e3
Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents:
26
diff
changeset
|
224 context.proc->port_handlers[PORT_CONTROLLER_2].read = controller_port_read; |
24 | 225 context.proc->port_handlers[PORT_FREQUENCY_A].write = frequency_port_write; |
226 context.proc->port_handlers[PORT_FREQUENCY_B].write = frequency_port_write; | |
227 context.proc->port_handlers[PORT_FREQUENCY_C].write = frequency_port_write; | |
228 context.proc->port_handlers[PORT_FREQUENCY_D].write = frequency_port_write; | |
229 context.proc->port_handlers[PORT_VOLUME_AB].write = volume_port_write; | |
230 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
|
231 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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 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
|
237 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
|
238 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
|
239 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
|
240 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
|
241 |
24 | 242 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
|
243 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
|
244 } |
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
|
245 |
24 | 246 context.audio = alloc_audio(MASTER_CLOCK, 17, system_sample_rate(), system_buffer_size()); |
247 | |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
248 run_console(&context); |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
249 return 0; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
250 } |