Minor edits made while composing the previous commit that added checksums.
[pdp11-bin2load] / custom_bitstream.c
/*
* © 2020 Aaron Taylor <ataylor at subgeniuskitty dot com>
* See LICENSE.txt file for copyright and license details.
*/
/*
* Outputs a file compatible with PDP-11 paper tape loader.
* File will load memory with integer counting (ex: 0, 1, 2, ..., NUMWORDS-2, NUMWORDS-1).
* Load starts at 01000 and ends at 01000 + NUMWORDS - 1.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define NUMWORDS 02000
int
main(int argc, char ** argv) {
uint16_t * block = malloc(2 * (NUMWORDS + 8));
/*
* +8 comes from:
* 3 for start of first block header
* 1 for first block checksum plus padding byte
* 4 for second block header + checksum on empty block
*/
block[0] = 1; /* Standard header start mark */
block[1] = (2 * NUMWORDS) + 6; /* Size of block, including header but excluding checksum */
block[2] = 01000; /* Address to load block */
block[NUMWORDS+4] = 1; /* Start of end-of-tape block mark */
block[NUMWORDS+5] = 6; /* Size of block (just the header since blank body */
block[NUMWORDS+6] = 01000; /* Blank body means start execution at this address */
block[NUMWORDS+7] = 0; /* Empty checksum */
for(uint16_t i = 0; i < NUMWORDS; i++) {
block[i + 3] = i; /* Start at 0 so first instruction halts machine */
}
uint32_t checksum = 0;
for(int i = 0; i < NUMWORDS+3; i++) {
checksum += block[i] & 0xff;
checksum += (block[i] >> 8) & 0xff;
}
checksum = (~checksum) + 1;
block[NUMWORDS+3] = checksum & 0xff; /* Checksum for first block */
FILE * fp;
fp = fopen("testimg.pdp11","w+");
fwrite(block,(2*(NUMWORDS+8)),1,fp);
fclose(fp);
}