-
Notifications
You must be signed in to change notification settings - Fork 83
/
loadsave.c
207 lines (182 loc) · 3.71 KB
/
loadsave.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Commander X16 Emulator
// Copyright (c) 2019 Michael Steil
// All rights reserved. License: 2-clause BSD
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include "glue.h"
#include "memory.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
static int
create_directory_listing(uint8_t *data)
{
uint8_t *data_start = data;
struct stat st;
DIR *dirp;
struct dirent *dp;
int file_size;
// We inject this directly into RAM, so
// this does not include the load address!
// link
*data++ = 1;
*data++ = 1;
// line number
*data++ = 0;
*data++ = 0;
*data++ = 0x12; // REVERSE ON
*data++ = '"';
for (int i = 0; i < 16; i++) {
*data++ = ' ';
}
if (!(getcwd((char *)data - 16, 256))) {
return false;
}
*data++ = '"';
*data++ = ' ';
*data++ = '0';
*data++ = '0';
*data++ = ' ';
*data++ = 'P';
*data++ = 'C';
*data++ = 0;
if (!(dirp = opendir("."))) {
return 0;
}
while ((dp = readdir(dirp))) {
size_t namlen = strlen(dp->d_name);
stat(dp->d_name, &st);
file_size = (st.st_size + 255)/256;
if (file_size > 0xFFFF) {
file_size = 0xFFFF;
}
// link
*data++ = 1;
*data++ = 1;
*data++ = file_size & 0xFF;
*data++ = file_size >> 8;
if (file_size < 1000) {
*data++ = ' ';
if (file_size < 100) {
*data++ = ' ';
if (file_size < 10) {
*data++ = ' ';
}
}
}
*data++ = '"';
if (namlen > 16) {
namlen = 16; // TODO hack
}
memcpy(data, dp->d_name, namlen);
data += namlen;
*data++ = '"';
for (int i = namlen; i < 16; i++) {
*data++ = ' ';
}
*data++ = ' ';
*data++ = 'P';
*data++ = 'R';
*data++ = 'G';
*data++ = 0;
}
// link
*data++ = 1;
*data++ = 1;
*data++ = 255; // "65535"
*data++ = 255;
char *blocks_free = "BLOCKS FREE.";
memcpy(data, blocks_free, strlen(blocks_free));
data += strlen(blocks_free);
*data++ = 0;
// link
*data++ = 0;
*data++ = 0;
(void)closedir(dirp);
return data - data_start;
}
void
LOAD()
{
char filename[41];
uint8_t len = MIN(RAM[0xb7], sizeof(filename) - 1);
memcpy(filename, (char *)&RAM[RAM[0xbb] | RAM[0xbc] << 8], len);
filename[len] = 0;
uint16_t override_start = (x | (y << 8));
if (filename[0] == '$') {
uint16_t dir_len = create_directory_listing(RAM + override_start);
uint16_t end = override_start + dir_len;
x = end & 0xff;
y = end >> 8;
status &= 0xfe;
RAM[0x90] = 0;
a = 0;
} else {
FILE *f = fopen(filename, "rb");
if (!f) {
a = 4; // FNF
RAM[0x90] = a;
status |= 1;
return;
}
uint8_t start_lo = fgetc(f);
uint8_t start_hi = fgetc(f);
uint16_t start;
if (!RAM[0xb9]) {
start = override_start;
} else {
start = start_hi << 8 | start_lo;
}
size_t bytes_read = 0;
if(start < 0x9f00) {
// Fixed RAM
bytes_read = fread(RAM + start, 1, 0x9f00 - start, f);
} else if(start < 0xa000) {
// IO addresses
} else if(start < 0xc000) {
// banked RAM
bytes_read = fread(RAM + ((uint16_t)memory_get_ram_bank() << 13) + start, 1, 0xc000 - start, f);
} else {
// ROM
}
fclose(f);
uint16_t end = start + bytes_read;
x = end & 0xff;
y = end >> 8;
status &= 0xfe;
RAM[0x90] = 0;
a = 0;
}
}
void
SAVE()
{
char filename[41];
uint8_t len = MIN(RAM[0xb7], sizeof(filename) - 1);
memcpy(filename, (char *)&RAM[RAM[0xbb] | RAM[0xbc] << 8], len);
filename[len] = 0;
uint16_t start = RAM[a] | RAM[a + 1] << 8;
uint16_t end = x | y << 8;
if (end < start) {
status |= 1;
a = 0;
return;
}
FILE *f = fopen(filename, "wb");
if (!f) {
a = 4; // FNF
RAM[0x90] = a;
status |= 1;
return;
}
fputc(start & 0xff, f);
fputc(start >> 8, f);
fwrite(RAM + start, 1, end - start, f);
fclose(f);
status &= 0xfe;
RAM[0x90] = 0;
a = 0;
}