-
Notifications
You must be signed in to change notification settings - Fork 0
/
apple1.c
164 lines (139 loc) · 3.54 KB
/
apple1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "tev.h"
#include "e6502.h"
#include "e6821.h"
#include "terminal.h"
#include "rom.h"
typedef struct
{
tev_handle_t tev;
terminal_t* term;
e6502_t cpu;
e6821_t pia;
pthread_t cpu_thread;
pthread_spinlock_t pia_lock;
uint8_t ram[0x10000];
} apple1_t;
static void* cpu_thread(void* ctx);
static void pia_lock(void* ctx);
static void pia_unlock(void* ctx);
static void write_to_term(uint8_t data, void* ctx);
static void on_term_input(char c, void* ctx);
static uint8_t on_cpu_load(uint16_t addr, void* ctx);
static void on_cpu_store(uint16_t addr, uint8_t data, void* ctx);
static int load_rom(const rom_block_t* rom, uint8_t* dst, size_t size);
static apple1_t this;
int main(int argc, char const *argv[])
{
memset(&this, 0, sizeof(apple1_t));
this.tev = tev_create_ctx();
if (!this.tev)
return -1;
this.term = terminal_get_singleton(this.tev);
if (!this.term)
{
tev_free_ctx(this.tev);
return -1;
}
if (pthread_spin_init(&this.pia_lock, PTHREAD_PROCESS_PRIVATE)!=0)
{
this.term->close();
tev_free_ctx(this.tev);
return -1;
}
e6821_init(&this.pia);
e6502_init(&this.cpu);
this.pia.callbacks.lock = pia_lock;
this.pia.callbacks.unlock = pia_unlock;
this.pia.callbacks.write_to_device_B = write_to_term;
this.term->callbacks.on_data = on_term_input;
this.cpu.cb.load = on_cpu_load;
this.cpu.cb.store = on_cpu_store;
for (int i = 0; i < sizeof(ROM_BLOCKS)/sizeof(rom_block_t); i++)
{
if (load_rom(&ROM_BLOCKS[i], this.ram, sizeof(this.ram)) != 0)
{
pthread_spin_destroy(&this.pia_lock);
this.term->close();
tev_free_ctx(this.tev);
return -1;
}
}
if (pthread_create(&this.cpu_thread, NULL, cpu_thread, NULL) != 0)
{
pthread_spin_destroy(&this.pia_lock);
this.term->close();
tev_free_ctx(this.tev);
return -1;
}
tev_main_loop(this.tev);
pthread_join(this.cpu_thread, NULL);
pthread_spin_destroy(&this.pia_lock);
/** term should have been closed */
tev_free_ctx(this.tev);
return 0;
}
static void* cpu_thread(void* ctx)
{
this.pia.iface.reset(&this.pia);
this.cpu.iface.reset(&this.cpu);
while(true)
{
this.cpu.iface.step(&this.cpu);
}
return NULL;
}
static void pia_lock(void* ctx)
{
pthread_spin_lock(&this.pia_lock);
}
static void pia_unlock(void* ctx)
{
pthread_spin_unlock(&this.pia_lock);
}
static void write_to_term(uint8_t data, void* ctx)
{
this.term->write(data);
}
static void on_term_input(char c, void* ctx)
{
this.pia.iface.input(&this.pia, E6821_PORT_A, c);
this.pia.iface.set_irq(&this.pia, E6821_PORT_A, E6821_IRQ_LINE_1);
}
static uint8_t on_cpu_load(uint16_t addr, void* ctx)
{
if ((addr & 0xF000) == 0xD000)
{
return this.pia.iface.read(&this.pia, addr & 0b11);
}
else
{
return this.ram[addr];
}
}
static void on_cpu_store(uint16_t addr, uint8_t data, void* ctx)
{
if ((addr & 0xF000) == 0xD000)
{
this.pia.iface.write(&this.pia, addr & 0x3, data);
}
else
{
/** @todo protect rom */
this.ram[addr] = data;
}
}
static int load_rom(const rom_block_t* rom, uint8_t* dst, size_t size)
{
if (rom->length + rom->offset > size)
{
return -1;
}
memcpy(dst + rom->offset, rom->data, rom->length);
return 0;
}