summaryrefslogtreecommitdiffstats
path: root/abc80.c
blob: 09cb6a62b1fa83cf1cc3504636d2e9eab24f6c11 (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
#include <stdio.h>
#include <string.h>

#define MAIN
#include "clock.h"
#include "screen.h"
#include "z80.h"
#include "rom.h"
#include "patchlevel.h"

static char __version_string[] = VERSION;

int events_in_queue = 1;
volatile int event_pending = 1;

/*
 * Read a two digit hex number from a string
 * and return its numeric value.
 */
static char *hexstring =  "0123456789ABCDEF";
static uint8_t gethex(char *p)
{
    return (uint8_t)(((strchr(hexstring, *p) - hexstring) << 4) 
          + (strchr(hexstring, *(p + 1)) - hexstring));
}


/*
 * Load in Intel-hex file into memory.
 * No checking of the checksum is performed.
 */
static void
load_sysfile(FILE *sysfile)
{
    uint8_t *memory;
    char  line[128];
    char *pos;
    int   len;
    int   type;

    while ( !feof(sysfile) ) {
        memory = mem_rom_address();
        fgets(line, 128, sysfile);
        if (line[0] != ':') {
            fprintf(stderr, "Invalid Intel-hex file.\n");
            exit(1);
        }
        pos = line + 1;
        len = gethex(pos); pos += 2;
        memory += (gethex(pos) << 8); pos += 2;
        memory += gethex(pos); pos += 2;
	type = gethex(pos); pos += 2;
	if (type == 1)
	    break;		/* End of file record */
	if (type != 0)
	    continue;		/* Not a data record */
	while (len--) {
            *memory++ = gethex(pos);
            pos += 2;
        }
    }
}

/*
 * Load an internal ROM
 */
static void load_rom(const struct rom *rom)
{
    uint8_t *memory = mem_rom_address();

    memcpy(memory + rom->offset, rom->data, rom->size);
}

/*
 * Load the BASIC interpretor into memory.
 */
static int no_basic = 0;

void load_basic(int mode40)
{
    if (!no_basic)
	load_rom(mode40 ? &abcrom40 : &abcrom80);
}

/*
 * Print usage message
 */
static void
usage(void)
{
    fprintf(stderr, "Usage: abc80 [-v] [-b] [-d] [hexfile...]\n");
    exit(1);
}


extern int   optind;
extern int   getopt(int, char **, char *);

int main(int argc, char **argv)
{
    char  sysfile_name[256];
    FILE *sysfile;
    int   no_device = 0;
    int   c;

    
    while ((c = getopt(argc, argv, "bdv")) != EOF) {
        switch (c) {

          case 'v':
            printf("ABC80 emulator version %s\n", __version_string);
            exit(0);
            break;

          case 'b':
            no_basic = 1;
            break;

          case 'd':
            no_device = 1;
            break;

          default:
            usage();
            exit(1);
            break;
        }
    }

    screen_init();                         
    mem_init();
    io_init();

    /*
     * Load the device driver code unless
     * we are asked not to.
     */
    if (!no_device) {
	load_rom(&ufddos);
	load_rom(&printrom);
    }

    /*
     * Load any other program files the
     * user gave on the command line.
     */
    while (optind < argc) {
        strcpy(sysfile_name, argv[optind]);
        if ((sysfile = fopen(sysfile_name, "r")) == NULL) {
            fprintf(stderr, "ABC80: Can't open file: %s\n", sysfile_name);
            exit(1);
        }
        load_sysfile(sysfile);
        fclose(sysfile);
        optind++;
    }

    /*
     * Off we go...
     */
    z80_reset();
    clock_init();

    z80_run(TRUE);

    screen_reset();
    exit(0);
}