Plan 9 from Bell Labs’s /usr/web/sources/contrib/aiju/nes/mmc1.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include "6502.h"
#include "nes.h"
#include "mmc1.h"

byte reg[4] = {0, 0, 0, 0};
byte state = 0, b = 0;

void mmc1reset(void) {
	state = 0;
	b = 0;
	reg[0] |= 0xC;
	memcpy(mem + 0xC000, prg[header.nprg - 1], 0x4000);
}

void mmc1updateprg(void) {
	int bank = (reg[3] & 0xf) % header.nprg;
	switch((reg[0]>>2) & 3) {
		case 0: case 1:
			memcpy(mem + 0x8000, prg[bank], 0x4000);
			memcpy(mem + 0xC000, prg[bank+1], 0x4000);
		break;
		case 2:
			memcpy(mem + 0x8000, prg[0], 0x4000);
			memcpy(mem + 0xC000, prg[bank], 0x4000);
		break;
		case 3:
			memcpy(mem + 0x8000, prg[bank], 0x4000);
			memcpy(mem + 0xC000, prg[header.nprg - 1], 0x4000);
		break;
	}
}

void mmc1write(word addr, byte val) {
	if(val & 0x80) {
		mmc1reset();
	}
	else {
		b = ((b >> 1) | (b << 7));
		b |= val & 1;
		state++;
		if(state == 5) {
			int r = (addr >> 13) - 4;
			b = ((b >> 4) | (b << 4));
			reg[r] = b;
			mmc1updateprg();
			state = 0;
			b = 0;
		}
	}
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.