9af10653e04f4d51c951aa1567e394398998cefc
[swan-dev] / tools / romheader.c
1 /**
2  * File              : romheader.c
3  * Author            : Robin Krens <robin@robinkrens.nl>
4  * Date              : 04.06.2022
5  * Last Modified Date: 04.06.2022
6  * Last Modified By  : Robin Krens <robin@robinkrens.nl>
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <string.h>
13
14 #define VERBOSE         1
15 #define SEGMENT_SIZE    0x10000
16
17 /* simple tool to pad 0xFF and to add ROM header
18  * romheader structure is as follows: */
19
20 struct __attribute__((__packed__)) rom_header {
21         uint8_t jmpf;
22         uint16_t label;
23         uint16_t segment;
24         uint8_t padding;
25         uint8_t dev_id;
26         uint8_t ws_type;
27         uint8_t cart_nr;
28         uint8_t padding2;
29         uint8_t romsize;
30         uint8_t ramsize;
31         uint8_t wsspec;
32         uint8_t padding3;
33         uint16_t checksum;
34 };
35
36 static struct rom_header header = {
37         .jmpf = 0xEA,
38         .label = 0x0,
39         .segment = 0xF000,
40         .dev_id = 0x42,
41         .ws_type = 0x01,
42         .cart_nr = 0x01,
43         .romsize = 0x03,
44         .ramsize = 0x00,
45         .wsspec = 0x04,
46         .checksum = 0x0000
47 };
48
49
50 int main(void)
51 {
52         FILE *fp;
53         /* open for appending, so seek postion is at end */
54         fp = fopen("test.wsc", "a");
55         if (fp == NULL) {
56                 perror("can not open file");
57                 exit(EXIT_FAILURE);
58         }
59
60         unsigned pos = ftell(fp);
61         int gap = (SEGMENT_SIZE - pos) - sizeof(struct rom_header);
62         if (gap <= 0)
63                 goto cleanup;
64
65         char dummy[SEGMENT_SIZE + 1];
66         memset(dummy, 0xFF, SEGMENT_SIZE);
67
68         unsigned wc = fwrite(dummy, sizeof(char), gap, fp);
69         wc += fwrite(&header, sizeof(char), sizeof(struct rom_header), fp);
70         
71         if (VERBOSE) {
72                 printf("size of struct: %ld\n", sizeof(struct rom_header));
73                 printf("pos: %d, gap to fill: %d\n", pos, gap);
74                 printf("written %d\n", wc);
75         }
76
77 cleanup:
78
79         fflush(fp);
80         fclose(fp);
81 }
82