aboutsummaryrefslogtreecommitdiffstats
path: root/bac80.c
blob: 741b18eacad669f122171cd2acae5271af996f3a (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "bin2abc.h"
#include "asmsrc/bac80.h"
#include "asmsrc/bac80r.h"

struct basic_block {
    FILE *out;
    int bytes;                  /* Bytes in buffer */
    int outbytes;               /* Bytes already written to file */
    int bofaoffs;               /* Current address relative to BOFA */
    uint16_t lineno;
    uint8_t *stringptr;        /* Accumulating string data, pointer to len */
    uint8_t data[253];
};

/* Finalize and output a data block */
static void writeblock(struct basic_block *blk, bool final)
{
    int bytes = blk->bytes;

    if (!bytes && !final)
        return;

    memset(blk->data + bytes + 1, 0, 252 - blk->bytes);
    blk->data[blk->bytes] = final ? 1 : 0;
    fwrite(blk->data, 1, 253, blk->out);

    blk->bofaoffs += blk->bytes;
    blk->outbytes += 253;
    blk->bytes = 0;
    blk->stringptr = NULL;
}

/*
 * Close a BASIC string being emitted.
 */
static void closestring(struct basic_block *blk)
{
    uint8_t *p = blk->data + blk->bytes;

    if (!blk->stringptr)
        return;

    *p++ = 0xbb;                /* Drop expression */
    *p++ = 0x0d;                /* Terminating CR */

    blk->bytes += 2;
    blk->stringptr = NULL;
}

/*
 * Emit data encapsulated in BASIC strings.  Multiple chunks can be
 * merged into common strings and strings can be broken up between
 * blocks.
 */
static void stringdata(struct basic_block *blk, const uint8_t * data, int len)
{
    while (len) {
        int copylen;
        uint8_t *p = blk->data + blk->bytes;

        if (!blk->stringptr) {
            if (blk->bytes >= 253 - 1 - 8) {
                /* Not enough space for any string */
                writeblock(blk, false);
                continue;
            }
            blk->stringptr = p;
            *p++ = 8;           /* Overhead per statement */
            p = set16(p, ++blk->lineno);
            *p++ = 0xcb;        /* String */
            *p++ = '"';         /* Which kind of quotation mark */
            *p++ = 0;           /* String length */
            blk->bytes += 6;
        }

        copylen = (253 - 1 - 2) - blk->bytes;
        if (copylen > len)
            copylen = len;

        if (copylen <= 0) {
            /* Not enough space left in this block for even one byte */
            closestring(blk);
            continue;
        }

        memcpy(p, data, copylen);

        p = blk->stringptr;
        p[0] += copylen;
        p[5] += copylen;

        blk->bytes += copylen;
        len -= copylen;
    }
}

/*
 * Emit a standalone BASIC statement, without encapsulating it in a string.
 */
static void bacstmt(struct basic_block *blk, const uint8_t * data, int len)
{
    uint8_t *p;

    if (blk->stringptr)
	closestring(blk);	/* Close any open string data */

    if (len > 253 - 1 - 4 - blk->bytes)
        writeblock(blk, false);

    p = &blk->data[blk->bytes];
    *p++ = len + 4;             /* Length of statement */
    p = set16(p, ++blk->lineno);        /* Line number */
    p = mempcpy(p, data, len);  /* Actual binary data */
    *p++ = 0x0d;                /* Terminating CR */

    blk->bytes = p - blk->data;
}

/*
 * BASIC statement for the original RUN
 *
 * PRINT CALL(PEEK(65054%)+SWAP%(65055%)-xxxx%)
 *
 * 65054 = EOFA, since we have the loader at the end
 */
static uint8_t run_prefix[] = {
    0x84, 0xc7, 0x1e, 0xfe, 0xce, 0x36, 0xc7, 0x1f,
    0xfe, 0xce, 0x36, 0xce, 0x34, 0xf5, 0xc7,
    0, 0,                       /* Loader length + 2 */
    0xf8, 0xce, 0x3a, 0xba
};
#define RUN_CALL_OFFSET 15

/*
 * This is the BASIC statement 1 POKE 65080%,CALL(yyyy%)
 * followed by an end of program marker (1 byte).  65080% is
 * the location of ERRCODE, which will be reset to -1 when
 * END is executed anyway, which will happen immediately.
 *
 * This includes the BASIC statement encapsulation, since this is
 * part of the payload.
 */
static uint8_t restart_prefix[] = {
    0x11, 0x01, 0x00, 0x86, 0x81, 0xc7,
    0x38, 0xfe,			/* Point to scratch byte (ERRCODE) */
    0xba, 0x2c, 0xc7,
    0, 0,			/* CALL address goes here */
    0xce, 0x3a, 0xba, 0x0d,
    0x01			/* End of BASIC program */
};
#define RESTART_CALL_OFFSET 11

/*
 * STOP statement, used as end-of-data marker.  Technically,
 * any BASIC statement of 3 bytes or less would work here.
 */
static const uint8_t stop_stmt[] = {
    0x86, 0x89			/* BASIC bytecode for STOP */
};

/*
 * Main routine
 */
int writebac80(FILE * out, const uint8_t * data, int len, int origin,
               int entrypoint, bool driver)
{
    struct basic_block blk;
    uint8_t *loader, *relocdata;
    int reloclen = 0;
    int loader_len;
    int loader_offset = 1;      /* 1 for the statement terminal CR */
    int pfxlen = 0;

    memset(&blk, 0, sizeof blk);
    blk.data[0] = 0x82;         /* Program start marker, not in memory */
    blk.bytes = 1;
    blk.out = out;

    if (have_relocs) {
        /* This is a relocatable binary, load it at BOFA */
        loader = bac80r_bin;
        loader_len = bac80r_SYM__end;
        loader_offset += (loader_len - bac80r_SYM__start);

        if (!driver) {
            /* Application, add a restart prefix to payload */
            pfxlen = sizeof restart_prefix;
            set16(restart_prefix + RESTART_CALL_OFFSET, entrypoint);
            push_reloc(RESTART_CALL_OFFSET - pfxlen);
        }
        loader[bac80r_SYM_DELTA] = pfxlen;
        set16(loader + bac80r_SYM_ENTRY, entrypoint + pfxlen);

        reloclen = pack_relocs(&relocdata, pfxlen, false);
        if (reloclen < 0)
            return -1;

        set16(loader + bac80r_SYM_NEG_RELOC_LEN, -(reloclen+1));
    } else {
        /* This is to be loaded at a fixed address */
        loader = bac80_bin;
        loader_len = bac80_SYM__end;
        loader_offset += (loader_len - bac80_SYM__start);

        set16(loader + bac80_SYM_ENTRY, entrypoint);
        set16(loader + bac80_SYM_LOAD, origin);
    }

    set16(run_prefix + RUN_CALL_OFFSET, loader_offset);

    /* Emit the RUN call prefix */
    bacstmt(&blk, run_prefix, sizeof run_prefix);

    /* Emit the restart prefix, if applicable */
    stringdata(&blk, restart_prefix, pfxlen);

    /* Emit the actual binary data */
    stringdata(&blk, data, len);

    /* Emit relocation information */
    if (have_relocs) {
        stringdata(&blk, (const uint8_t *)"\1", 1);     /* EOFA marker */
        stringdata(&blk, relocdata, reloclen);
    }

    /* Emit STOP statement to indicate end of data blocks */
    bacstmt(&blk, stop_stmt, sizeof stop_stmt);

    /* Emit loader */
    bacstmt(&blk, loader, loader_len);

    /* Write the final block */
    closestring(&blk);
    writeblock(&blk, true);

    return 0;
}