Index: firmware/main_aeon_lite.c
===================================================================
--- firmware/main_aeon_lite.c	(revision 179)
+++ firmware/main_aeon_lite.c	(nonexistent)
@@ -1,3 +0,0 @@
-#include "aeon_lite/memory.h"
-#include "main_real.c"
-
Index: firmware/main_real.c
===================================================================
--- firmware/main_real.c	(revision 179)
+++ firmware/main_real.c	(nonexistent)
@@ -1,612 +0,0 @@
-#include "integer.h"
-#include "regs.h"
-#include "pause.h"
-#include "printf.h"
-#include "joystick.h"
-#include "freeze.h"
-
-#include "simpledir.h"
-#include "simplefile.h"
-#include "fileselector.h"
-
-#include "atari_drive_emulator.h"
-
-extern char ROM_DIR[];
-
-// FUNCTIONS in here
-// i) pff init - NOT USED EVERYWHERE
-// ii) file selector - kind of crap, no fine scrolling - NOT USED EVERYWHERE
-// iii) cold reset atari (clears base ram...)
-// iv) start atari (begins paused)
-// v) freeze/resume atari - NOT USED EVERYWHERE!
-// vi) menu for various options - NOT USED EVERYWHERE!
-// vii) pause - TODO - base this on pokey clock...
-
-// standard ZPU IN/OUT use...
-// OUT1 - 6502 settings (pause,reset,speed)
-// pause_n: bit 0 
-// reset_n: bit 1
-// turbo: bit 2-4: meaning... 0=1.79Mhz,1=3.58MHz,2=7.16MHz,3=14.32MHz,4=28.64MHz,5=57.28MHz,etc.
-// ram_select: bit 5-7: 
-//   		RAM_SELECT : in std_logic_vector(2 downto 0); -- 64K,128K,320KB Compy, 320KB Rambo, 576K Compy, 576K Rambo, 1088K, 4MB
-// rom_select: bit 8-13:
-//    		ROM_SELECT : in std_logic_vector(5 downto 0); -- 16KB ROM Bank
-
-#define BIT_REG(op,mask,shift,name,reg) \
-int get_ ## name() \
-{ \
-	int val = *reg; \
-	return op((val>>shift)&mask); \
-} \
-void set_ ## name(int param) \
-{ \
-	int val = *reg; \
-	 \
-	val = (val&~(mask<<shift)); \
-	val |= op(param)<<shift; \
-	 \
-	*reg = val; \
-}
-
-#define BIT_REG_RO(op,mask,shift,name,reg) \
-int get_ ## name() \
-{ \
-	int val = *reg; \
-	return op((val>>shift)&mask); \
-}
-
-BIT_REG(,0x1,0,pause_6502,zpu_out1)
-BIT_REG(,0x1,1,reset_6502,zpu_out1)
-BIT_REG(,0x3f,2,turbo_6502,zpu_out1)
-BIT_REG(,0x7,8,ram_select,zpu_out1)
-BIT_REG(,0x3f,11,rom_select,zpu_out1)
-
-BIT_REG_RO(,0x1,8,hotkey_softboot,zpu_in1)
-BIT_REG_RO(,0x1,9,hotkey_coldboot,zpu_in1)
-BIT_REG_RO(,0x1,10,hotkey_fileselect,zpu_in1)
-BIT_REG_RO(,0x1,11,hotkey_settings,zpu_in1)
-
-void
-wait_us(int unsigned num)
-{
-	// pause counter runs at pokey frequency - should be 1.79MHz
-	int unsigned cycles = (num*230)>>7;
-	*zpu_pause = cycles;
-}
-
-void memset8(void * address, int value, int length)
-{
-	char * mem = address;
-	while (length--)
-		*mem++=value;
-}
-
-void memset32(void * address, int value, int length)
-{
-	int * mem = address;
-	while (length--)
-		*mem++=value;
-}
-
-void clear_64k_ram()
-{
-	memset8((void *)0x200000, 0, 65536); // SRAM, if present (TODO)
-	memset32((void *)0x800000, 0, 16384);
-}
-
-void
-reboot(int cold)
-{
-	set_pause_6502(1);
-	if (cold)
-	{
-		clear_64k_ram();
-	}
-	set_reset_6502(1);
-	// Do nothing in here - this resets the memory controller!
-	set_reset_6502(0);
-	set_pause_6502(0);
-}
-
-unsigned char toatarichar(int val)
-{
-	int inv = val>=128;
-	if (inv)
-	{
-		val-=128;
-	}
-	if (val>='A' && val<='Z')
-	{
-		val+=-'A'+33;
-	}
-	else if (val>='a' && val<='z')
-	{
-		val+=-'a'+33+64;
-	}
-	else if (val>='0' && val<='9')
-	{
-		val+=-'0'+16;	
-	}
-	else if (val>=32 && val<=47)
-	{
-		val+=-32;
-	}
-	else
-	{
-		val = 0;
-	}
-	if (inv)
-	{
-		val+=128;
-	}
-	return val;
-}
-
-int debug_pos;
-int debug_adjust;
-unsigned char volatile * baseaddr;
-
-void clearscreen()
-{
-	unsigned volatile char * screen;
-	for (screen=(unsigned volatile char *)40000+atari_regbase; screen!=(unsigned volatile char *)(atari_regbase+40000+1024); ++screen)
-		*screen = 0x00;
-}
-
-void char_out ( void* p, char c)
-{
-	unsigned char val = toatarichar(c);
-	if (debug_pos>=0)
-	{
-		*(baseaddr+debug_pos) = val|debug_adjust;
-		++debug_pos;
-	}
-}
-
-struct SimpleFile * files[6];
-void loadromfile(struct SimpleFile * file, int size, void * ram_address)
-{
-	ram_address += 0x800000;
-	int read = 0;
-	file_read(file, ram_address, size, &read);
-}
-
-void loadrom(char const * path, int size, void * ram_address)
-{
-	if (SimpleFile_OK == file_open_name(path, files[4]))
-	{
-		loadromfile(files[4], size, ram_address);
-	}
-}
-
-void loadrom_indir(struct SimpleDirEntry * entries, char const * filename, int size, void * ram_address)
-{
-	if (SimpleFile_OK == file_open_name_in_dir(entries, filename, files[4]))
-	{
-		loadromfile(files[4], size, ram_address);
-	}
-}
-
-void loadosrom()
-{
-	if (file_size(files[5]) == 0x4000)
-	{
-		loadromfile(files[5],0x4000, (void *)(ROM_MEM + 0x4000));
-	}
-	else if (file_size(files[5]) ==0x2800)
-	{
-		loadromfile(files[5],0x2800, (void *)(ROM_MEM + 0x5800));
-	}
-}
-
-int main(void)
-{
-/*	disk_initialize();
-	{
-		char buffer[512];
-
-		freeze_init((void*)FREEZE_MEM); // 128k
-	
-		debug_pos = -1;
-		debug_adjust = 0;
-		baseaddr = (unsigned char volatile *)(40000 + atari_regbase);
-		set_pause_6502(1);
-		set_reset_6502(1);
-		set_reset_6502(0);
-		set_turbo_6502(1);
-		set_rom_select(1);
-		set_ram_select(0);
-	
-		init_printf(0, char_out);
-		reboot(1);
-	//	for (;;);
-		wait_us(5000000);
-
-		set_pause_6502(1);
-		freeze();
-		debug_pos = 0;	
-
-		printf("Hello world 3");
-		debug_pos = 40;
-		int i;
-		for (i=0;i!=512; ++i)
-		{
-			buffer[i] = i;
-		}
-		int volatile * x = (int volatile *)0x4000;
-		int volatile * y = (int volatile *)&buffer[0];
-		for (i=0;i!=128; ++i)
-		{
-			x[i] = y[i];
-		}
-		hexdump_pure(0x4000,8);
-		hexdump_pure(0x41f8,8);
-		debug_pos = 120;
-
-		printf(" Writing sector ");
-		n_actual_mmc_sector = 30;
-		disk_writeflush();
-		printf(" Wrote sector ");
-
-		for (i=0;i!=128; ++i)
-		{
-			x[i] = 0;
-		}
-		n_actual_mmc_sector = -1;
-		disk_readp(&buffer[0],30,0,512);
-		hexdump_pure(0x4000,8);
-		hexdump_pure(0x41f8,8);
-		debug_pos = 200;
-
-		disk_readp(&buffer[0],0x103,0,512);
-		hexdump_pure(0x4000,8);
-		hexdump_pure(0x41f8,8);
-		debug_pos = 280;
-
-		disk_readp(&buffer[0],0,0,512);
-		hexdump_pure(0x4000,8);
-		hexdump_pure(0x41f8,8);
-		debug_pos = 360;
-
-		printf("OK...");
-
-		//for (;;);
-		wait_us(10000000);
-	}*/
-
-/*	spiInit();
-	set_spi_clock_freq();
-	mmcReadLoop();*/
-
-/*	spiInit();
-	set_spi_clock_freq();
-	char buffer[512];
-	while (1)
-	{
-		mmcReadLoop();
-		wait_us(500);
-	}*/
-
-	int i;
-	for (i=0; i!=6; ++i)
-	{
-		files[i] = (struct SimpleFile *)alloca(file_struct_size());
-		file_init(files[i]);
-	}
-
-	freeze_init((void*)FREEZE_MEM); // 128k
-
-	debug_pos = -1;
-	debug_adjust = 0;
-	baseaddr = (unsigned char volatile *)(40000 + atari_regbase);
-	set_pause_6502(1);
-	set_reset_6502(1);
-	set_reset_6502(0);
-	set_turbo_6502(1);
-	set_rom_select(1);
-	set_ram_select(2);
-
-	init_printf(0, char_out);
-
-	// TODO...
-
-	if (SimpleFile_OK == dir_init((void *)DIR_INIT_MEM, DIR_INIT_MEMSIZE))
-	{
-	//	printf("DIR init ok\n");
-		init_drive_emulator();
-
-		
-		struct SimpleDirEntry * entries = dir_entries(ROM_DIR);
-		
-		//loadrom_indir(entries,"atarixl.rom",0x4000, (void *)0x704000);
-		if (SimpleFile_OK == file_open_name_in_dir(entries, "atarixl.rom", files[5]))
-		{
-			loadosrom();
-		}
-
-		/*loadrom_indir(entries,"xlhias.rom",0x4000, (void *)0x708000);
-		loadrom_indir(entries,"ultimon.rom",0x4000, (void *)0x70c000);
-		loadrom_indir(entries,"osbhias.rom",0x4000, (void *)0x710000);
-		loadrom_indir(entries,"osborig.rom",0x2800, (void *)0x715800);
-		loadrom_indir(entries,"osaorig.rom",0x2800, (void *)0x719800);*/
-
-		loadrom_indir(entries,"ataribas.rom",0x2000,(void *)ROM_MEM);
-
-		//ROM = xlorig.rom,0x4000, (void *)0x704000
-		//ROM = xlhias.rom,0x4000, (void *)0x708000
-		//ROM = ultimon.rom,0x4000, (void *)0x70c000
-		//ROM = osbhias.rom,0x4000, (void *)0x710000
-		//ROM = osborig.rom,0x2800, (void *)0x715800
-		//ROM = osaorig.rom,0x2800, (void *)0x719800
-		//
-		//ROM = ataribas.rom,0x2000,(void *)0x700000
-
-		//--SDRAM_BASIC_ROM_ADDR <= "111"&"000000"   &"00000000000000";
-		//--SDRAM_OS_ROM_ADDR    <= "111"&rom_select &"00000000000000";
-		reboot(1);
-		run_drive_emulator();
-	}
-	else
-	{
-		//printf("DIR init failed\n");
-	}
-	reboot(1);
-	for (;;) actions();
-}
-
-//		struct SimpleFile * file = alloca(file_struct_size());
-//		printf("Opening file\n");
-//		if (SimpleFile_OK == file_open_name("GUNPOWDR.ATR",file))
-//		{
-//			printf("FILE open ok\n");
-//
-//			set_drive_status(0,file);
-//
-//		}
-//		else
-//		{
-//			printf("FILE open failed\n");
-//		}
-
-char const * get_ram()
-{
-	static char const * ram[] = 
-	{
-		"64K",
-		"128K",
-		"320K(Compy)",
-		"320K(Rambo)",
-		"576K(Compy)",
-		"576K(Rambo)",
-		"1MB",
-		"4MB"
-	};
-	return ram[get_ram_select()];
-	/*switch(get_ram_select())
-	{
-	case 0:
-		return "64K";
-	case 1:
-		return "128K";
-	case 2:
-		return "320K(Compy)";
-	case 3:
-		return "320K(Rambo)";
-	case 4:
-		return "576K(Compy)";
-	case 5:
-		return "576K(Rambo)";
-	case 6:
-		return "1MB";
-	case 7:
-		return "4MB";
-	}*/
-}
-
-int settings()
-{
-	struct joystick_status joy;
-	joy.x_ = joy.y_ = joy.fire_ = 0;
-
-	int row = 0;
-
-	int done = 0;
-	for (;!done;)
-	{
-		// Render
-		clearscreen();
-		debug_pos = 0;
-		debug_adjust = 0;
-		printf("Se");
-		debug_adjust = 128;
-		printf("ttings");
-		debug_pos = 80;
-		debug_adjust = row==0 ? 128 : 0;
-		printf("Turbo:%dx", get_turbo_6502());
-		debug_pos = 120;
-		debug_adjust = row==1 ? 128 : 0;
-		printf("Ram:%s", get_ram());
-		debug_pos = 160;
-		debug_adjust = row==2 ? 128 : 0;
-		{
-			printf("Rom:%s", file_name(files[5]));
-		}
-		debug_pos = 240;
-		int i;
-		for (i=1;i!=5;++i)
-		{
-			int temp = debug_pos;
-			debug_adjust = row==i+2 ? 128 : 0;
-			char buffer[20];
-			describe_disk(i-1,&buffer[0]);
-			printf("Drive %d:%s %s", i, file_name(files[i-1]), &buffer[0]);
-			debug_pos = temp+40;
-		}
-
-		debug_pos = 400;
-		debug_adjust = row==7 ? 128 : 0;
-		printf("Cartridge 8k simple");
-
-		debug_pos = 480;
-		debug_adjust = row==8 ? 128 : 0;
-		printf("Exit");
-
-		// Slow it down a bit
-		wait_us(100000);
-
-		// move
-		joystick_wait(&joy,WAIT_QUIET);
-		joystick_wait(&joy,WAIT_EITHER);
-
-		row+=joy.y_;
-		if (row<0) row = 0;
-		if (row>8) row = 8;
-		switch (row)
-		{
-		case 0:
-			{
-				int turbo = get_turbo_6502();
-				if (joy.x_==1) turbo<<=1;
-				if (joy.x_==-1) turbo>>=1;
-				if (turbo>16) turbo = 16;
-				if (turbo<1) turbo = 1;
-				set_turbo_6502(turbo);
-			}
-			break;
-		case 1:
-			{
-				int ram_select = get_ram_select();
-				ram_select+=joy.x_;
-				if (ram_select<0) ram_select = 0;
-				if (ram_select>7) ram_select = 7;
-				set_ram_select(ram_select);
-			}
-			break;
-		case 2:
-			{
-				if (joy.x_ || joy.fire_)
-				{
-					filter = filter_roms;
-					file_selector(files[5]);
-					loadosrom();
-				}
-			}
-			break;
-		case 3:
-		case 4:
-		case 5:
-		case 6:
-			{
-				if (joy.x_>0)
-				{
-					// Choose new disk
-					filter = filter_disks;
-					file_selector(files[row-3]);
-					set_drive_status(row-3,files[row-3]);
-				}
-				else if(joy.x_<0)
-				{
-					// Remove disk
-					file_init(files[row-3]);
-					set_drive_status(row-3,0);
-				}
-				else if (joy.fire_)
-				{
-					{
-						// Swap files
-						struct SimpleFile * temp = files[row-3];
-						files[row-3] = files[0];
-						files[0] = temp;
-					}
-
-					{
-						// Swap disks
-						struct SimpleFile * temp = get_drive_status(row-3);
-						set_drive_status(row-3, get_drive_status(0));
-						set_drive_status(0,temp);
-					}
-				}
-			}
-			break;
-		case 7:
-			{
-				if (joy.fire_)
-				{
-					filter = filter_roms;
-					file_selector(files[4]);
-					loadrom(file_name(files[4]),0x2000,(void *)0x700000);
-					return 1;
-				}
-			}
-			break;
-		case 8:
-			if (joy.fire_)
-			{
-				done = 1;
-			}
-			break;
-		}
-	}
-
-	return 0;
-}
-
-void actions()
-{
-	// Show some activity!
-	//*atari_colbk = *atari_random;
-	
-	// Hot keys
-	if (get_hotkey_softboot())
-	{
-		reboot(0);	
-	}
-	else if (get_hotkey_coldboot())
-	{
-		reboot(1);	
-	}
-	else if (get_hotkey_settings())
-	{
-		set_pause_6502(1);
-		freeze();
-		debug_pos = 0;	
-		int do_reboot = settings();
-		debug_pos = -1;
-		restore();
-		if (do_reboot)
-			reboot(1);
-		else
-			set_pause_6502(0);
-	}
-	else if (get_hotkey_fileselect())
-	{
-		set_pause_6502(1);
-		freeze();
-		filter = filter_disks;
-		file_selector(files[0]);
-		debug_pos = -1;
-		restore();
-		set_drive_status(0,files[0]);
-		reboot(1);
-
-
-/*	disk_initialize();
-	{
-		char buffer[512];
-		set_pause_6502(1);
-		freeze();
-		debug_pos = 0;	
-
-		printf("Hello world 3");
-		debug_pos = 40;
-		n_actual_mmc_sector = -1;
-		disk_readp(&buffer[0],0,0,512);
-		hexdump_pure(&buffer[0],512);
-
-		wait_us(10000000);
-		restore();
-	}*/
-	}
-}
-
-
Index: firmware/Makefile
===================================================================
--- firmware/Makefile	(revision 179)
+++ firmware/Makefile	(revision 180)
@@ -14,39 +14,53 @@
 
 BUILD_DIR=zpu_obj
 
+AEON_LITE_DIR = aeon_lite
+DE1_DIR = de1
+CHAMELEON_DIR = chameleon
+MCC_DIR = mcc
+MIST_DIR = mist
+
+AEON_LITE_BUILD_DIR = $(BUILD_DIR)/$(AEON_LITE_DIR)
+DE1_BUILD_DIR = $(BUILD_DIR)/$(DE1_DIR)
+CHAMELEON_BUILD_DIR = $(BUILD_DIR)/$(CHAMELEON_DIR)
+MCC_BUILD_DIR = $(BUILD_DIR)/$(MCC_DIR)
+MIST_BUILD_DIR = $(BUILD_DIR)/$(MIST_DIR)
+
 #MINSTARTUP_SRC = mincrt0.s
 MINSTARTUP_SRC = mycrt0.s
 MINSTARTUP_OBJ = $(patsubst $(STARTUP_DIR)/%.s,$(BUILD_DIR)/%.o,$(MINSTARTUP_SRC))
 
+COMMON_SRC = main.c regs.c freeze.c joystick.c fileutils.c fileselector.c atari_drive_emulator.c pokey/uart.c hexdump.c printf/printf.c fat/pff_file.c fat/pff.c common/utils.c
+SDCARD_SRC = sd_direct/diskio_mmc.c sd_direct/spi.c sd_direct/mmc2.c
+
 AEON_LITE_PRJ = AEON_LITE
-AEON_LITE_SRC = main_aeon_lite.c regs.c freeze.c joystick.c fileutils.c fileselector.c atari_drive_emulator.c pokey/uart.c hexdump.c printf/printf.c fat/pff_file.c fat/pff.c common/utils.c sd_direct/diskio_mmc.c sd_direct/spi.c sd_direct/mmc2.c de1/dirs.c
-AEON_LITE_OBJ = $(patsubst %.c,$(BUILD_DIR)/%.o,$(AEON_LITE_SRC))
+AEON_LITE_SRC = $(COMMON_SRC) $(SDCARD_SRC) de1/dirs.c
+AEON_LITE_OBJ = $(patsubst %.c,$(AEON_LITE_BUILD_DIR)/%.o,$(AEON_LITE_SRC))
 
 DE1_PRJ = DE1
-DE1_SRC = main.c regs.c freeze.c joystick.c fileutils.c fileselector.c atari_drive_emulator.c pokey/uart.c hexdump.c printf/printf.c fat/pff_file.c fat/pff.c common/utils.c sd_direct/diskio_mmc.c sd_direct/spi.c sd_direct/mmc2.c de1/dirs.c
-DE1_OBJ = $(patsubst %.c,$(BUILD_DIR)/%.o,$(DE1_SRC))
+DE1_SRC = $(COMMON_SRC) $(SDCARD_SRC) de1/dirs.c
+DE1_OBJ = $(patsubst %.c,$(DE1_BUILD_DIR)/%.o,$(DE1_SRC))
 
 CHAMELEON_PRJ = CHAMELEON
-CHAMELEON_SRC = main.c regs.c freeze.c joystick.c fileutils.c fileselector.c atari_drive_emulator.c pokey/uart.c hexdump.c printf/printf.c fat/pff_file.c fat/pff.c common/utils.c sd_direct/diskio_mmc.c sd_direct/spi.c sd_direct/mmc2.c chameleon/dirs.c
-CHAMELEON_OBJ = $(patsubst %.c,$(BUILD_DIR)/%.o,$(CHAMELEON_SRC))
+CHAMELEON_SRC = $(COMMON_SRC) $(SDCARD_SRC) chameleon/dirs.c
+CHAMELEON_OBJ = $(patsubst %.c,$(CHAMELEON_BUILD_DIR)/%.o,$(CHAMELEON_SRC))
 
 MCC_PRJ = MCC216
-MCC_SRC = main.c regs.c freeze.c joystick.c fileutils.c fileselector.c atari_drive_emulator.c pokey/uart.c hexdump.c printf/printf.c fat/pff_file.c fat/pff.c common/utils.c sd_direct/diskio_mmc.c sd_direct/spi.c sd_direct/mmc2.c mcc/dirs.c
-MCC_OBJ = $(patsubst %.c,$(BUILD_DIR)/%.o,$(MCC_SRC))
+MCC_SRC = $(COMMON_SRC) $(SDCARD_SRC) mcc/dirs.c
+MCC_OBJ = $(patsubst %.c,$(MCC_BUILD_DIR)/%.o,$(MCC_SRC))
 
 MIST_PRJ = MIST
-MIST_SRC = main.c regs.c freeze.c joystick.c fileutils.c fileselector.c atari_drive_emulator.c pokey/uart.c hexdump.c printf/printf.c fat/pff_file.c fat/pff.c common/utils.c mist/diskio_sectorrequest.c mist/dirs.c
-MIST_OBJ = $(patsubst %.c,$(BUILD_DIR)/%.o,$(MIST_SRC))
+MIST_SRC = $(COMMON_SRC) mist/diskio_sectorrequest.c mist/dirs.c
+MIST_OBJ = $(patsubst %.c,$(MIST_BUILD_DIR)/%.o,$(MIST_SRC))
 
 LINKMAP  = ./standalone_simple.ld
 
-
 # Commandline options for each tool.
 
 #ZPUOPTS= -mno-poppcrel -mno-pushspadd -mno-callpcrel -mno-shortop -mno-neg # No-neg requires bugfixed toolchain
 #Include everything -> need to include emulation rom...
 ZPUOPTS =
-CFLAGS  = -I. -Isd_direct -Iprintf -Ifat -Icommon -c -g -Os $(ZPUOPTS) -DDISABLE_UART_RX
+CFLAGS  = -I. -Isd_direct -Iprintf -Ifat -Icommon -Isdram_common -c -g -Os $(ZPUOPTS) -DDISABLE_UART_RX
 
 LFLAGS  = -nostartfiles -Wl,--relax -g -Os
 #LFLAGS  = -nostartfiles -Os
@@ -102,10 +116,26 @@
 $(MIST_PRJ).elf: $(MINSTARTUP_OBJ) $(MIST_OBJ)
 	$(LD) $(LFLAGS) -T $(LINKMAP) -o $@ $+ $(LIBS)
 
-$(BUILD_DIR)/%.o: %.c Makefile
+$(AEON_LITE_BUILD_DIR)/%.o: %.c Makefile
 	mkdir -p `dirname $@`
-	$(CC) $(CFLAGS)  -o $@ -c $<
+	$(CC) -I$(AEON_LITE_DIR) $(CFLAGS)  -o $@ -c $<
 
+$(CHAMELEON_BUILD_DIR)/%.o: %.c Makefile
+	mkdir -p `dirname $@`
+	$(CC) -I$(CHAMELEON_DIR) $(CFLAGS)  -o $@ -c $<
+
+$(DE1_BUILD_DIR)/%.o: %.c Makefile
+	mkdir -p `dirname $@`
+	$(CC) -I$(DE1_DIR) $(CFLAGS)  -o $@ -c $<
+
+$(MCC_BUILD_DIR)/%.o: %.c Makefile
+	mkdir -p `dirname $@`
+	$(CC) -I$(MCC_DIR) $(CFLAGS)  -o $@ -c $<
+
+$(MIST_BUILD_DIR)/%.o: %.c Makefile
+	mkdir -p `dirname $@`
+	$(CC) -I$(MIST_DIR) $(CFLAGS)  -o $@ -c $<
+
 $(BUILD_DIR)/%.o: %.s
 	$(AS) -o $@ $<
 
Index: firmware/aeon_lite/memory.h
===================================================================
--- firmware/aeon_lite/memory.h	(revision 179)
+++ firmware/aeon_lite/memory.h	(revision 180)
@@ -1,12 +1,23 @@
-#pragma once
+#ifndef MEMORY_H
+#define MEMORY_H
 
+#define SRAM_BASE ((void*) 0x200000)
+#define SDRAM_BASE ((void*) 0x800000)
+
 // Memory usage...
 // 0x90000-0x9FFFF (0x890000 in zpu space) = directory cache - 64k
 // 0xA0000-0xBFFFF (0x8a0000 in zpu space) = freeze backup
 // 0xC0000-0xDFFFF (0x8c0000 in zpu space) = os rom/basic rom
 
-#define DIR_INIT_MEM 0x890000
+#define DIR_INIT_MEM (SDRAM_BASE + 0x90000)
 #define DIR_INIT_MEMSIZE 65536
-#define FREEZE_MEM 0x8a0000
-#define ROM_MEM 0xc0000
+#define FREEZE_MEM (SDRAM_BASE + 0xa0000)
 
+// offset in SDRAM area
+#define ROM_OFS 0xc0000
+
+#define atari_regbase  ((void*) 0x10000)
+#define atari_regmirror  ((void*) 0x20000)
+#define config_regbase ((void*) 0x40000)
+
+#endif
Index: firmware/freeze.c
===================================================================
--- firmware/freeze.c	(revision 179)
+++ firmware/freeze.c	(revision 180)
@@ -1,6 +1,7 @@
 #include "freeze.h"
 
 #include "regs.h"
+#include "memory.h"
 
 unsigned char store_portb;
 unsigned volatile char * store_mem;
Index: firmware/main.c
===================================================================
--- firmware/main.c	(revision 179)
+++ firmware/main.c	(revision 180)
@@ -1,3 +1,614 @@
-#include "sdram_common/memory.h"
-#include "main_real.c"
+#include <sys/types.h>
+#include "integer.h"
+#include "regs.h"
+#include "pause.h"
+#include "printf.h"
+#include "joystick.h"
+#include "freeze.h"
 
+#include "simpledir.h"
+#include "simplefile.h"
+#include "fileselector.h"
+
+#include "atari_drive_emulator.h"
+#include "memory.h"
+
+extern char ROM_DIR[];
+
+// FUNCTIONS in here
+// i) pff init - NOT USED EVERYWHERE
+// ii) file selector - kind of crap, no fine scrolling - NOT USED EVERYWHERE
+// iii) cold reset atari (clears base ram...)
+// iv) start atari (begins paused)
+// v) freeze/resume atari - NOT USED EVERYWHERE!
+// vi) menu for various options - NOT USED EVERYWHERE!
+// vii) pause - TODO - base this on pokey clock...
+
+// standard ZPU IN/OUT use...
+// OUT1 - 6502 settings (pause,reset,speed)
+// pause_n: bit 0 
+// reset_n: bit 1
+// turbo: bit 2-4: meaning... 0=1.79Mhz,1=3.58MHz,2=7.16MHz,3=14.32MHz,4=28.64MHz,5=57.28MHz,etc.
+// ram_select: bit 5-7: 
+//   		RAM_SELECT : in std_logic_vector(2 downto 0); -- 64K,128K,320KB Compy, 320KB Rambo, 576K Compy, 576K Rambo, 1088K, 4MB
+// rom_select: bit 8-13:
+//    		ROM_SELECT : in std_logic_vector(5 downto 0); -- 16KB ROM Bank
+
+#define BIT_REG(op,mask,shift,name,reg) \
+int get_ ## name() \
+{ \
+	int val = *reg; \
+	return op((val>>shift)&mask); \
+} \
+void set_ ## name(int param) \
+{ \
+	int val = *reg; \
+	 \
+	val = (val&~(mask<<shift)); \
+	val |= op(param)<<shift; \
+	 \
+	*reg = val; \
+}
+
+#define BIT_REG_RO(op,mask,shift,name,reg) \
+int get_ ## name() \
+{ \
+	int val = *reg; \
+	return op((val>>shift)&mask); \
+}
+
+BIT_REG(,0x1,0,pause_6502,zpu_out1)
+BIT_REG(,0x1,1,reset_6502,zpu_out1)
+BIT_REG(,0x3f,2,turbo_6502,zpu_out1)
+BIT_REG(,0x7,8,ram_select,zpu_out1)
+BIT_REG(,0x3f,11,rom_select,zpu_out1)
+
+BIT_REG_RO(,0x1,8,hotkey_softboot,zpu_in1)
+BIT_REG_RO(,0x1,9,hotkey_coldboot,zpu_in1)
+BIT_REG_RO(,0x1,10,hotkey_fileselect,zpu_in1)
+BIT_REG_RO(,0x1,11,hotkey_settings,zpu_in1)
+
+void
+wait_us(int unsigned num)
+{
+	// pause counter runs at pokey frequency - should be 1.79MHz
+	int unsigned cycles = (num*230)>>7;
+	*zpu_pause = cycles;
+}
+
+void memset8(void * address, int value, int length)
+{
+	char * mem = address;
+	while (length--)
+		*mem++=value;
+}
+
+void memset32(void * address, int value, int length)
+{
+	int * mem = address;
+	while (length--)
+		*mem++=value;
+}
+
+void clear_64k_ram()
+{
+	memset8(SRAM_BASE, 0, 65536); // SRAM, if present (TODO)
+	memset32(SDRAM_BASE, 0, 16384);
+}
+
+void
+reboot(int cold)
+{
+	set_pause_6502(1);
+	if (cold)
+	{
+		clear_64k_ram();
+	}
+	set_reset_6502(1);
+	// Do nothing in here - this resets the memory controller!
+	set_reset_6502(0);
+	set_pause_6502(0);
+}
+
+unsigned char toatarichar(int val)
+{
+	int inv = val>=128;
+	if (inv)
+	{
+		val-=128;
+	}
+	if (val>='A' && val<='Z')
+	{
+		val+=-'A'+33;
+	}
+	else if (val>='a' && val<='z')
+	{
+		val+=-'a'+33+64;
+	}
+	else if (val>='0' && val<='9')
+	{
+		val+=-'0'+16;	
+	}
+	else if (val>=32 && val<=47)
+	{
+		val+=-32;
+	}
+	else
+	{
+		val = 0;
+	}
+	if (inv)
+	{
+		val+=128;
+	}
+	return val;
+}
+
+int debug_pos;
+int debug_adjust;
+unsigned char volatile * baseaddr;
+
+void clearscreen()
+{
+	unsigned volatile char * screen;
+	for (screen=(unsigned volatile char *)(40000+atari_regbase); screen!=(unsigned volatile char *)(atari_regbase+40000+1024); ++screen)
+		*screen = 0x00;
+}
+
+void char_out ( void* p, char c)
+{
+	unsigned char val = toatarichar(c);
+	if (debug_pos>=0)
+	{
+		*(baseaddr+debug_pos) = val|debug_adjust;
+		++debug_pos;
+	}
+}
+
+struct SimpleFile * files[6];
+void loadromfile(struct SimpleFile * file, int size, size_t ram_address)
+{
+	void* absolute_ram_address = SDRAM_BASE + ram_address;
+	int read = 0;
+	file_read(file, absolute_ram_address, size, &read);
+}
+
+void loadrom(char const * path, int size, size_t ram_address)
+{
+	if (SimpleFile_OK == file_open_name(path, files[4]))
+	{
+		loadromfile(files[4], size, ram_address);
+	}
+}
+
+void loadrom_indir(struct SimpleDirEntry * entries, char const * filename, int size, size_t ram_address)
+{
+	if (SimpleFile_OK == file_open_name_in_dir(entries, filename, files[4]))
+	{
+		loadromfile(files[4], size, ram_address);
+	}
+}
+
+void loadosrom()
+{
+	if (file_size(files[5]) == 0x4000)
+	{
+		loadromfile(files[5],0x4000, ROM_OFS + 0x4000);
+	}
+	else if (file_size(files[5]) ==0x2800)
+	{
+		loadromfile(files[5],0x2800, ROM_OFS + 0x5800);
+	}
+}
+
+int main(void)
+{
+/*	disk_initialize();
+	{
+		char buffer[512];
+
+		freeze_init((void*)FREEZE_MEM); // 128k
+	
+		debug_pos = -1;
+		debug_adjust = 0;
+		baseaddr = (unsigned char volatile *)(40000 + atari_regbase);
+		set_pause_6502(1);
+		set_reset_6502(1);
+		set_reset_6502(0);
+		set_turbo_6502(1);
+		set_rom_select(1);
+		set_ram_select(0);
+	
+		init_printf(0, char_out);
+		reboot(1);
+	//	for (;;);
+		wait_us(5000000);
+
+		set_pause_6502(1);
+		freeze();
+		debug_pos = 0;	
+
+		printf("Hello world 3");
+		debug_pos = 40;
+		int i;
+		for (i=0;i!=512; ++i)
+		{
+			buffer[i] = i;
+		}
+		int volatile * x = (int volatile *)0x4000;
+		int volatile * y = (int volatile *)&buffer[0];
+		for (i=0;i!=128; ++i)
+		{
+			x[i] = y[i];
+		}
+		hexdump_pure(0x4000,8);
+		hexdump_pure(0x41f8,8);
+		debug_pos = 120;
+
+		printf(" Writing sector ");
+		n_actual_mmc_sector = 30;
+		disk_writeflush();
+		printf(" Wrote sector ");
+
+		for (i=0;i!=128; ++i)
+		{
+			x[i] = 0;
+		}
+		n_actual_mmc_sector = -1;
+		disk_readp(&buffer[0],30,0,512);
+		hexdump_pure(0x4000,8);
+		hexdump_pure(0x41f8,8);
+		debug_pos = 200;
+
+		disk_readp(&buffer[0],0x103,0,512);
+		hexdump_pure(0x4000,8);
+		hexdump_pure(0x41f8,8);
+		debug_pos = 280;
+
+		disk_readp(&buffer[0],0,0,512);
+		hexdump_pure(0x4000,8);
+		hexdump_pure(0x41f8,8);
+		debug_pos = 360;
+
+		printf("OK...");
+
+		//for (;;);
+		wait_us(10000000);
+	}*/
+
+/*	spiInit();
+	set_spi_clock_freq();
+	mmcReadLoop();*/
+
+/*	spiInit();
+	set_spi_clock_freq();
+	char buffer[512];
+	while (1)
+	{
+		mmcReadLoop();
+		wait_us(500);
+	}*/
+
+	int i;
+	for (i=0; i!=6; ++i)
+	{
+		files[i] = (struct SimpleFile *)alloca(file_struct_size());
+		file_init(files[i]);
+	}
+
+	freeze_init((void*)FREEZE_MEM); // 128k
+
+	debug_pos = -1;
+	debug_adjust = 0;
+	baseaddr = (unsigned char volatile *)(40000 + atari_regbase);
+	set_pause_6502(1);
+	set_reset_6502(1);
+	set_reset_6502(0);
+	set_turbo_6502(1);
+	set_rom_select(1);
+	set_ram_select(2);
+
+	init_printf(0, char_out);
+
+	// TODO...
+
+	if (SimpleFile_OK == dir_init((void *)DIR_INIT_MEM, DIR_INIT_MEMSIZE))
+	{
+	//	printf("DIR init ok\n");
+		init_drive_emulator();
+
+		
+		struct SimpleDirEntry * entries = dir_entries(ROM_DIR);
+		
+		//loadrom_indir(entries,"atarixl.rom",0x4000, (void *)0x704000);
+		if (SimpleFile_OK == file_open_name_in_dir(entries, "atarixl.rom", files[5]))
+		{
+			loadosrom();
+		}
+
+		/*loadrom_indir(entries,"xlhias.rom",0x4000, (void *)0x708000);
+		loadrom_indir(entries,"ultimon.rom",0x4000, (void *)0x70c000);
+		loadrom_indir(entries,"osbhias.rom",0x4000, (void *)0x710000);
+		loadrom_indir(entries,"osborig.rom",0x2800, (void *)0x715800);
+		loadrom_indir(entries,"osaorig.rom",0x2800, (void *)0x719800);*/
+
+		loadrom_indir(entries,"ataribas.rom",0x2000,ROM_OFS);
+
+		//ROM = xlorig.rom,0x4000, (void *)0x704000
+		//ROM = xlhias.rom,0x4000, (void *)0x708000
+		//ROM = ultimon.rom,0x4000, (void *)0x70c000
+		//ROM = osbhias.rom,0x4000, (void *)0x710000
+		//ROM = osborig.rom,0x2800, (void *)0x715800
+		//ROM = osaorig.rom,0x2800, (void *)0x719800
+		//
+		//ROM = ataribas.rom,0x2000,(void *)0x700000
+
+		//--SDRAM_BASIC_ROM_ADDR <= "111"&"000000"   &"00000000000000";
+		//--SDRAM_OS_ROM_ADDR    <= "111"&rom_select &"00000000000000";
+		reboot(1);
+		run_drive_emulator();
+	}
+	else
+	{
+		//printf("DIR init failed\n");
+	}
+	reboot(1);
+	for (;;) actions();
+}
+
+//		struct SimpleFile * file = alloca(file_struct_size());
+//		printf("Opening file\n");
+//		if (SimpleFile_OK == file_open_name("GUNPOWDR.ATR",file))
+//		{
+//			printf("FILE open ok\n");
+//
+//			set_drive_status(0,file);
+//
+//		}
+//		else
+//		{
+//			printf("FILE open failed\n");
+//		}
+
+char const * get_ram()
+{
+	static char const * ram[] = 
+	{
+		"64K",
+		"128K",
+		"320K(Compy)",
+		"320K(Rambo)",
+		"576K(Compy)",
+		"576K(Rambo)",
+		"1MB",
+		"4MB"
+	};
+	return ram[get_ram_select()];
+	/*switch(get_ram_select())
+	{
+	case 0:
+		return "64K";
+	case 1:
+		return "128K";
+	case 2:
+		return "320K(Compy)";
+	case 3:
+		return "320K(Rambo)";
+	case 4:
+		return "576K(Compy)";
+	case 5:
+		return "576K(Rambo)";
+	case 6:
+		return "1MB";
+	case 7:
+		return "4MB";
+	}*/
+}
+
+int settings()
+{
+	struct joystick_status joy;
+	joy.x_ = joy.y_ = joy.fire_ = 0;
+
+	int row = 0;
+
+	int done = 0;
+	for (;!done;)
+	{
+		// Render
+		clearscreen();
+		debug_pos = 0;
+		debug_adjust = 0;
+		printf("Se");
+		debug_adjust = 128;
+		printf("ttings");
+		debug_pos = 80;
+		debug_adjust = row==0 ? 128 : 0;
+		printf("Turbo:%dx", get_turbo_6502());
+		debug_pos = 120;
+		debug_adjust = row==1 ? 128 : 0;
+		printf("Ram:%s", get_ram());
+		debug_pos = 160;
+		debug_adjust = row==2 ? 128 : 0;
+		{
+			printf("Rom:%s", file_name(files[5]));
+		}
+		debug_pos = 240;
+		int i;
+		for (i=1;i!=5;++i)
+		{
+			int temp = debug_pos;
+			debug_adjust = row==i+2 ? 128 : 0;
+			char buffer[20];
+			describe_disk(i-1,&buffer[0]);
+			printf("Drive %d:%s %s", i, file_name(files[i-1]), &buffer[0]);
+			debug_pos = temp+40;
+		}
+
+		debug_pos = 400;
+		debug_adjust = row==7 ? 128 : 0;
+		printf("Cartridge 8k simple");
+
+		debug_pos = 480;
+		debug_adjust = row==8 ? 128 : 0;
+		printf("Exit");
+
+		// Slow it down a bit
+		wait_us(100000);
+
+		// move
+		joystick_wait(&joy,WAIT_QUIET);
+		joystick_wait(&joy,WAIT_EITHER);
+
+		row+=joy.y_;
+		if (row<0) row = 0;
+		if (row>8) row = 8;
+		switch (row)
+		{
+		case 0:
+			{
+				int turbo = get_turbo_6502();
+				if (joy.x_==1) turbo<<=1;
+				if (joy.x_==-1) turbo>>=1;
+				if (turbo>16) turbo = 16;
+				if (turbo<1) turbo = 1;
+				set_turbo_6502(turbo);
+			}
+			break;
+		case 1:
+			{
+				int ram_select = get_ram_select();
+				ram_select+=joy.x_;
+				if (ram_select<0) ram_select = 0;
+				if (ram_select>7) ram_select = 7;
+				set_ram_select(ram_select);
+			}
+			break;
+		case 2:
+			{
+				if (joy.x_ || joy.fire_)
+				{
+					filter = filter_roms;
+					file_selector(files[5]);
+					loadosrom();
+				}
+			}
+			break;
+		case 3:
+		case 4:
+		case 5:
+		case 6:
+			{
+				if (joy.x_>0)
+				{
+					// Choose new disk
+					filter = filter_disks;
+					file_selector(files[row-3]);
+					set_drive_status(row-3,files[row-3]);
+				}
+				else if(joy.x_<0)
+				{
+					// Remove disk
+					file_init(files[row-3]);
+					set_drive_status(row-3,0);
+				}
+				else if (joy.fire_)
+				{
+					{
+						// Swap files
+						struct SimpleFile * temp = files[row-3];
+						files[row-3] = files[0];
+						files[0] = temp;
+					}
+
+					{
+						// Swap disks
+						struct SimpleFile * temp = get_drive_status(row-3);
+						set_drive_status(row-3, get_drive_status(0));
+						set_drive_status(0,temp);
+					}
+				}
+			}
+			break;
+		case 7:
+			{
+				if (joy.fire_)
+				{
+					filter = filter_roms;
+					file_selector(files[4]);
+					loadrom(file_name(files[4]),0x2000,0x700000);
+					return 1;
+				}
+			}
+			break;
+		case 8:
+			if (joy.fire_)
+			{
+				done = 1;
+			}
+			break;
+		}
+	}
+
+	return 0;
+}
+
+void actions()
+{
+	// Show some activity!
+	//*atari_colbk = *atari_random;
+	
+	// Hot keys
+	if (get_hotkey_softboot())
+	{
+		reboot(0);	
+	}
+	else if (get_hotkey_coldboot())
+	{
+		reboot(1);	
+	}
+	else if (get_hotkey_settings())
+	{
+		set_pause_6502(1);
+		freeze();
+		debug_pos = 0;	
+		int do_reboot = settings();
+		debug_pos = -1;
+		restore();
+		if (do_reboot)
+			reboot(1);
+		else
+			set_pause_6502(0);
+	}
+	else if (get_hotkey_fileselect())
+	{
+		set_pause_6502(1);
+		freeze();
+		filter = filter_disks;
+		file_selector(files[0]);
+		debug_pos = -1;
+		restore();
+		set_drive_status(0,files[0]);
+		reboot(1);
+
+
+/*	disk_initialize();
+	{
+		char buffer[512];
+		set_pause_6502(1);
+		freeze();
+		debug_pos = 0;	
+
+		printf("Hello world 3");
+		debug_pos = 40;
+		n_actual_mmc_sector = -1;
+		disk_readp(&buffer[0],0,0,512);
+		hexdump_pure(&buffer[0],512);
+
+		wait_us(10000000);
+		restore();
+	}*/
+	}
+}
+
+
Index: firmware/regs.c
===================================================================
--- firmware/regs.c	(revision 179)
+++ firmware/regs.c	(revision 180)
@@ -1,6 +1,6 @@
 #include "regs.h"
+#include "memory.h"
 
-#define config_regbase 0x40000
 int volatile * zpu_in1 = (int *)(0*4+config_regbase);
 int volatile * zpu_in2 = (int *)(1*4+config_regbase);
 int volatile * zpu_in3 = (int *)(2*4+config_regbase);
Index: firmware/regs.h
===================================================================
--- firmware/regs.h	(revision 179)
+++ firmware/regs.h	(revision 180)
@@ -1,9 +1,6 @@
 #ifndef regs_h
 #define regs_h
 
-#define atari_regbase  0x10000
-#define atari_regmirror  0x20000
-
 extern int volatile * zpu_in1;
 extern int volatile * zpu_in2;
 extern int volatile * zpu_in3;
Index: firmware/sdram_common/memory.h
===================================================================
--- firmware/sdram_common/memory.h	(revision 179)
+++ firmware/sdram_common/memory.h	(revision 180)
@@ -1,12 +1,23 @@
-#pragma once
+#ifndef MEMORY_H
+#define MEMORY_H
 
+#define SRAM_BASE ((void*) 0x200000)
+#define SDRAM_BASE ((void*) 0x800000)
+
 // Memory usage...
 // 0x410000-0x44FFFF (0xc10000 in zpu space) = directory cache - 256k
 // 0x450000-0x46FFFF (0xc50000 in zpu space) = freeze backup
 // 0x700000-0x77FFFF (0xf00000 in zpu space) = os rom/basic rom
 
-#define DIR_INIT_MEM 0xc10000
+#define DIR_INIT_MEM (SDRAM_BASE + 0x410000)
 #define DIR_INIT_MEMSIZE 262144
-#define FREEZE_MEM 0xc50000
-#define ROM_MEM 0x700000
+#define FREEZE_MEM (SDRAM_BASE + 0x450000)
 
+// offset into SDRAM
+#define ROM_OFS 0x700000
+
+#define atari_regbase  ((void*) 0x10000)
+#define atari_regmirror  ((void*) 0x20000)
+#define config_regbase ((void*) 0x40000)
+
+#endif
