First pass at parsing MEMLIST.BIN!

This commit is contained in:
kvothe. 2020-08-22 03:53:35 +00:00
parent f808997db8
commit 2c91ae32af
4 changed files with 93 additions and 35 deletions

View File

@ -1,42 +1,9 @@
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <bio.h>
#include <mach.h>
/* TODO: What'd I tell you about that coupling? */
Point ballPos = {40, 40};
#include "memlist.h"
void main(void)
{
char filename[] = "assets/BANK01";
int fd = open(filename, OREAD);
if ( !fd ) {
exits("couldn't open the file!");
}
Dir *filestats = dirfstat(fd);
if ( filestats == nil ) {
exits("couldn't fstat the file!");
}
vlong filesize = filestats->length;
print("\"%s\" has size %lld.\n", filename, filesize);
free(filestats);
if ( !machbyname("68020") ) {
exits("libmach doesn't support \"68020\" architecture.");
}
Map *bankmap = newmap(nil, 1);
if (bankmap == nil) {
exits("couldn't allocate a new map.");
}
setmap(bankmap, fd, 0, filesize, 0, "bankmap");
ulong readval = 0;
int result = get4(bankmap, 0, &readval);
print("get4() returned %d and read 0x%08lx.\n", result, readval);
if (fd) {
close(fd);
}
free(bankmap);
loadmemlist("assets/MEMLIST.BIN");
exits(nil);
}

57
memlist.c Normal file
View File

@ -0,0 +1,57 @@
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>
#include "memlist.h"
void
loadmemlist(char *filename)
{
int fd = open(filename, OREAD);
if ( !fd ) {
exits("couldn't open the file!");
}
Dir *filestats = dirfstat(fd);
if ( filestats == nil ) {
exits("couldn't fstat the file!");
}
vlong filesize = filestats->length;
print("\"%s\" has size %lld.\n", filename, filesize);
free(filestats);
if ( !machbyname("68020") ) {
exits("libmach doesn't support \"68020\" architecture.");
}
Map *bankmap = newmap(nil, 1);
if (bankmap == nil) {
exits("couldn't allocate a new map.");
}
setmap(bankmap, fd, 0, filesize, 0, "memlist");
uvlong offset = 0;
for ( uint assetno = 0; assetno < 10; assetno++ ) {
Asset readAsset = {0};
get1(bankmap, offset, (uchar*) &readAsset.state, 1); offset += 1;
get1(bankmap, offset, (uchar*) &readAsset.type, 1); offset += 1; offset += 4;
get1(bankmap, offset, (uchar*) &readAsset.rank, 1); offset += 1;
get1(bankmap, offset, (uchar*) &readAsset.bank, 1); offset += 1;
get4(bankmap, offset, (ulong*) &readAsset.bankoffset ); offset += 4; offset += 2;
get2(bankmap, offset, (ushort*) &readAsset.packedlength); offset += 2; offset += 2;
get2(bankmap, offset, (ushort*) &readAsset.length ); offset += 2;
USED(offset); /* this is stupid */
print("--[asset %d]--\n", assetno);
print("state: 0x%02X.\n", readAsset.state);
print("type: 0x%02X.\n", readAsset.type);
print("rank: 0x%02X.\n", readAsset.rank);
print("bank: 0x%02X.\n", readAsset.bank);
print("offset: %lld.\n", readAsset.bankoffset);
print("packed: %d.\n", readAsset.packedlength);
print("length: %d.\n", readAsset.length);
}
if (fd) {
close(fd);
}
free(bankmap);
}

32
memlist.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef MEMLIST_H
#define MEMLIST_H
typedef enum AssetState_t {
ASSET_NOT_NEEDED = 0,
ASSET_LOADED = 1,
ASSET_UNLOADED = 2,
ASSET_END = 255,
} AssetState;
typedef enum AssetType_t {
ASSET_SOUND = 0,
ASSET_MUSIC = 1,
ASSET_FRAME = 2,
ASSET_PALETTE = 3,
ASSET_BYTECODE = 4,
ASSET_VIDEO = 5,
} AssetType;
typedef struct Asset_t {
AssetState state;
AssetType type;
uchar * data;
uchar rank;
uchar bank;
uvlong bankoffset;
ushort packedlength;
ushort length;
} Asset;
void loadmemlist(char *filename);
#endif /* MEMLIST_H */

2
mkfile
View File

@ -2,9 +2,11 @@
TARG=anotherplan anotherplan_tests
OFILES=\
graphics.$O\
memlist.$O\
HFILES=\
graphics.h\
memlist.h\
BIN=/$objtype/bin/games
</sys/src/cmd/mkmany