
void writeIo(u8 addr, u8 value, u8 timing = 4) {
	_sync(timing);
	ioWrite(addr, value);
}

u8 readIo(u8 addr, u8 timing = 3) {
	_sync(timing - 1); //set address on bus
	mdr = ioRead(addr);
	_sync(1);
	return mdr;
}

void writeByte(u16 addr, u8 value, u8 timing = 3) {
	_sync(timing);
	memWrite(addr, value);
}

void writeStack(u16 value, u8 timing = 3) {
	writeByte(--reg.w.SP, value >> 8, timing);
	writeByte(--reg.w.SP, value & 0xFF, timing);
}

u8 readByte(u16 addr, u8 timing = 3, bool fetch = false) {
	u8 needTiming = !fetch ? timing - 1 : 2;
	if (needTiming > timing) needTiming = timing;
	if (needTiming != 0) _sync( needTiming );
	mdr = memRead(addr);
	needTiming = timing - needTiming;
	if (needTiming != 0) _sync( needTiming );
	return mdr;
}

u8 readPc(u8 timing = 4, bool fetch = false) {
	u8 data = readByte(reg.w.PC, timing, fetch);
	reg.w.PC++;
	return data;
}

u16 readStack(u8 timing = 3) {
	u16 data;
	data = readByte(reg.w.SP++, timing);
	data |= readByte(reg.w.SP++, timing) << 8;
	return data;
}

