/*
 * Z80SIM  -  a Z80-CPU simulator
 *
 * Copyright (C) 2016 by Udo Munk
 *
 * This module implements memory management for a Cromemco Z-1 system
 *
 * History:
 * 22-NOV-16 stuff moved to here and implemented as inline functions
 */

extern void wait_step(void), wait_int_step(void);

extern int common;
extern BYTE *mmu[MAXSEG];

/*
 * memory writer: write byte data into memory @ addr
 */
static inline void memwrt(BYTE *addr, BYTE data)
{
	register int i;

#ifdef BUS_8080
	cpu_bus &= ~(CPU_WO | CPU_MEMR);
#endif
#ifdef FRONTPANEL
	fp_clock += 20;
	fp_led_address = addr - ram;
	fp_led_data = 0xff;
	fp_sampleData();
	wait_step();
#endif
	/* write data into the CPU addressable memory */
	*addr = data;

	/* if common writes not active we are done */
	if (!common)
		return;

	/* common active, might need write data into user banks 1-6 too */
	if (addr >= ram + 32768) {
		//printf("user bank write %04x\r\n", (unsigned int)(addr-ram));
		for (i = 1; i < MAXSEG; i++)
			*(mmu[i] + (unsigned int) (addr - ram)) = data;
	}
}

/*
 * memory reader: return byte from memory @ addr
 */
static inline BYTE memrdr(BYTE *addr)
{
#ifdef BUS_8080
	cpu_bus |= CPU_WO | CPU_MEMR;
#endif
#ifdef FRONTPANEL
	fp_clock += 20;
	fp_led_address = addr - ram;
	fp_led_data = *addr;
	fp_sampleData();
	wait_step();
#endif
	return(*addr);
}
