annotate src/main.c @ 58:bed2d84eeabe

Fix ldimh special casing of loading labels for the case when the label is not a forward reference
author Michael Pavone <pavone@retrodev.com>
date Tue, 06 Sep 2016 09:42:31 -0700
parents 6e7bfe83d2b0
children
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"
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
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
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
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
15 uint8_t rom[4 * 1024 * 1024];
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
16 uint8_t ram[128 * 1024];
0
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,
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
31 PORT_START_OFFSET,
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
32 PORT_VIDEO_MODE,
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
33 PORT_CRAM,
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
34 RESERVED_3
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
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
55 void offset_port_write(cpu *context, uint8_t port, uint16_t value)
8
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);
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
59 system->video.start_offset = value;
8
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
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
62 uint16_t offset_port_read(cpu *context, uint8_t port)
8
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
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
69 void mode_port_write(cpu *context, uint8_t port, uint16_t value)
8
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);
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
73 vdp_write_mode(&system->video, value);
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
74 context->mem_regions[2].base = vdp_get_back_buffer(&system->video);
8
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
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
77 uint16_t mode_port_read(cpu *context, uint8_t port)
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
78 {
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
79 console *system = context->system;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
80 vdp_run(&system->video, context->cycles);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
81 return system->video.hcounter;
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
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
84 void cram_port_write(cpu *context, uint8_t port, uint16_t value)
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
85 {
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
86 console *system = context->system;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
87 vdp_run(&system->video, context->cycles);
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
88 vdp_write_cram(&system->video, value);
8
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
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
91 uint16_t cram_port_read(cpu *context, uint8_t port)
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
92 {
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
93 console *system = context->system;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
94 vdp_run(&system->video, context->cycles);
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
95 return system->video.status;
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
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
98 void frequency_port_write(cpu *context, uint8_t port, uint16_t value)
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
99 {
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
100 console *system = context->system;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
101 audio_run(system->audio, context->cycles);
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
102 audio_write_freq(system->audio, port - PORT_FREQUENCY_A, 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
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
105 void volume_port_write(cpu *context, uint8_t port, uint16_t value)
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
106 {
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
107 console *system = context->system;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
108 audio_run(system->audio, context->cycles);
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
109 audio_write_vol(system->audio, port - PORT_VOLUME_AB, 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
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
112 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
113 {
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
114 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
115 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
116 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
117 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
118 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
119 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
120 }
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 }
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
28
c677507682e3 Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents: 26
diff changeset
123 uint16_t controller_port_read(cpu *context, uint8_t port)
c677507682e3 Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents: 26
diff changeset
124 {
c677507682e3 Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents: 26
diff changeset
125 //process events so controller state is as fresh as possible
c677507682e3 Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents: 26
diff changeset
126 system_poll_events();
c677507682e3 Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents: 26
diff changeset
127 console *system = context->system;
c677507682e3 Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents: 26
diff changeset
128 return controller_read(&system->pads, port - PORT_CONTROLLER_1);
c677507682e3 Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents: 26
diff changeset
129 }
c677507682e3 Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents: 26
diff changeset
130
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
131 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
132 {
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 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
134 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
135 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
136 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
137 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
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 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
140 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
141 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
142 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
143 next = vnext;
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
144 }
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
145 }
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 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
147 }
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
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 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
150 {
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 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
152 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
153 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
154 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
155 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
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 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
158 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
159 bits |= 2;
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
160 }
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
161 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
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
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 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
165 {
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
166 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
167 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
168 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
169 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
170 } else {
083347ccd508 Implemented vblank interrupts and fixed a bug in exception vector address calculation
Michael Pavone <pavone@retrodev.com>
parents: 25
diff changeset
171 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
172 }
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 }
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
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
175 memory_region regions[] = {
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
176 { .base = rom, .start = 0, .end = sizeof(rom)-1, .mask = 0x7FFFFF, .flags = MEM_READ },
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
177 { .base = ram, .start = sizeof(rom), .end = 0x4FFFFF, .mask = 0xFFFF, .flags = MEM_READ|MEM_WRITE },
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
178 { .base = NULL, .start = 0x700000, .end = 0x7FFFFF, .mask = 0xFFFF, .flags = 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
179 };
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
180
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
181 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
182 {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
183 for(;;)
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
184 {
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
185 run_cpu(context->proc, CYCLES_PER_FRAME);
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
186 audio_run(context->audio, CYCLES_PER_FRAME);
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
187 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
188 timer_run(&context->timer, CYCLES_PER_FRAME);
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
189 context->proc->cycles -= CYCLES_PER_FRAME;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
190 context->video.cycles -= CYCLES_PER_FRAME;
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
191 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
192 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
193 system_poll_events();
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
194 }
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
195 }
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
196
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
197 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
198 {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
199 if (argc < 2) {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
200 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
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 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
204 if (!f) {
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
205 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
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
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
209 size_t read;
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
210 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
211 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
212 }
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
213 fclose(f);
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
214 console context;
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
215 context.proc = alloc_cpu(2, sizeof(regions)/sizeof(memory_region), regions);
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
216 context.proc->system = &context;
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
217 vdp_init(&context.video, 2);
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
218 context.proc->mem_regions[2].base = vdp_get_back_buffer(&context.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
219 timer_init(&context.timer, 16);
33
eda4919d955f Forgot to call controller_init
Michael Pavone <pavone@retrodev.com>
parents: 30
diff changeset
220 controller_init(&context.pads);
28
c677507682e3 Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents: 26
diff changeset
221 context.proc->port_handlers[PORT_CONTROLLER_1].read = controller_port_read;
c677507682e3 Untested controller implementation
Michael Pavone <pavone@retrodev.com>
parents: 26
diff changeset
222 context.proc->port_handlers[PORT_CONTROLLER_2].read = controller_port_read;
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
223 context.proc->port_handlers[PORT_FREQUENCY_A].write = frequency_port_write;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
224 context.proc->port_handlers[PORT_FREQUENCY_B].write = frequency_port_write;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
225 context.proc->port_handlers[PORT_FREQUENCY_C].write = frequency_port_write;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
226 context.proc->port_handlers[PORT_FREQUENCY_D].write = frequency_port_write;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
227 context.proc->port_handlers[PORT_VOLUME_AB].write = volume_port_write;
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
228 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
229 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
230 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
231 context.proc->port_handlers[PORT_SERIAL].read = debug_port_read;
43
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
232 context.proc->port_handlers[PORT_START_OFFSET].write = offset_port_write;
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
233 context.proc->port_handlers[PORT_START_OFFSET].read = offset_port_read;
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
234 context.proc->port_handlers[PORT_VIDEO_MODE].write = mode_port_write;
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
235 context.proc->port_handlers[PORT_VIDEO_MODE].read = mode_port_read;
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
236 context.proc->port_handlers[PORT_CRAM].write = cram_port_write;
6e7bfe83d2b0 Changed the design to vastly simplify the video hardware and support a 23-bit address space on the CPU
Michael Pavone <pavone@retrodev.com>
parents: 33
diff changeset
237 context.proc->port_handlers[PORT_CRAM].read = cram_port_read;
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
238
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
239 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
240 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
241 }
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
242
24
4c9dbfa30a66 Implemented audio
Michael Pavone <pavone@retrodev.com>
parents: 19
diff changeset
243 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
244
8
5176efdda5ae Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents: 0
diff changeset
245 run_console(&context);
0
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
246 return 0;
7e44f7d5810b Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
247 }