GE-115 Emulator
An Emulator of the General Electrics GE-115 computer
binimage.h
Go to the documentation of this file.
1#ifndef BINIMAGE_H
2#define BINIMAGE_H
3/*
4 * binimage.h — the GE-120 "unified binary format".
5 *
6 * A small big-endian header (machine-native byte order) followed by a flat
7 * machine-code image. This is the single interchange format shared by the
8 * toolchain:
9 *
10 * gasm (assembler) emits it
11 * gdis (disassembler) emits it (depunched .cap / --bin -> loadable image)
12 * gemu (emulator) loads it (positional `./ge prog.bin`)
13 *
14 * The same header is what a minimal real-machine card-reader bootstrap parses
15 * to place the image at `origin` and jump to `entry`.
16 *
17 * Layout (12-byte header, then `length` image bytes):
18 *
19 * off size field notes
20 * 0 4 magic 'G','E','1','2' (0x47 0x45 0x31 0x32)
21 * 4 1 version BINIMAGE_VERSION
22 * 5 1 flags reserved, must be 0
23 * 6 2 origin load address (big-endian)
24 * 8 2 entry initial PO (big-endian)
25 * 10 2 length image byte count (big-endian)
26 * 12 ... image flat machine code, `length` bytes
27 *
28 * The header is metadata only: it is never placed in emulated/real memory.
29 *
30 * This file is standalone (only <stdio.h>/<stdint.h>/<stddef.h>); it includes
31 * no gemu/gasm/gdis header so every tool can compile binimage.c directly.
32 */
33
34#include <stdio.h>
35#include <stdint.h>
36#include <stddef.h>
37
38#define BINIMAGE_MAGIC0 0x47u /* 'G' */
39#define BINIMAGE_MAGIC1 0x45u /* 'E' */
40#define BINIMAGE_MAGIC2 0x31u /* '1' */
41#define BINIMAGE_MAGIC3 0x32u /* '2' */
42#define BINIMAGE_VERSION 0x01u
43#define BINIMAGE_HDR_SIZE 12
44
45/* Result codes (0 = success). */
46enum {
48 BINIMAGE_E_IO = -1, /* read/write error */
49 BINIMAGE_E_MAGIC = -2, /* bad magic */
50 BINIMAGE_E_VERSION = -3, /* unsupported version */
51 BINIMAGE_E_TRUNCATED = -4, /* header/image shorter than `length` */
52 BINIMAGE_E_TOOBIG = -5, /* image larger than caller's buffer */
53 BINIMAGE_E_RANGE = -6 /* origin + length would exceed 0x10000 */
54};
55
56/*
57 * Write a unified-format file: the 12-byte header followed by `len` image
58 * bytes. `fp` must be opened in binary write mode. Returns BINIMAGE_OK or a
59 * negative error code.
60 */
61int binimage_write(FILE *fp, uint16_t origin, uint16_t entry,
62 const uint8_t *img, uint16_t len);
63
64/*
65 * Read a unified-format file. On success *origin, *entry and *len are filled
66 * and up to *len image bytes are copied into buf (which must hold >= the
67 * stored length; bufcap is the caller's capacity). Returns BINIMAGE_OK or a
68 * negative error code. `fp` must be opened in binary read mode.
69 */
70int binimage_read(FILE *fp, uint16_t *origin, uint16_t *entry,
71 uint8_t *buf, size_t bufcap, uint16_t *len);
72
73/* Human-readable message for a binimage_* result code. */
74const char *binimage_strerror(int code);
75
76#endif /* BINIMAGE_H */
@ BINIMAGE_E_VERSION
Definition binimage.h:50
@ BINIMAGE_OK
Definition binimage.h:47
@ BINIMAGE_E_IO
Definition binimage.h:48
@ BINIMAGE_E_RANGE
Definition binimage.h:53
@ BINIMAGE_E_MAGIC
Definition binimage.h:49
@ BINIMAGE_E_TRUNCATED
Definition binimage.h:51
@ BINIMAGE_E_TOOBIG
Definition binimage.h:52
int binimage_write(FILE *fp, uint16_t origin, uint16_t entry, const uint8_t *img, uint16_t len)
Definition binimage.c:10
const char * binimage_strerror(int code)
Definition binimage.c:79
int binimage_read(FILE *fp, uint16_t *origin, uint16_t *entry, uint8_t *buf, size_t bufcap, uint16_t *len)
Definition binimage.c:41