program Tutorial_1_hello_nes;

/*

  You need to have installed the NES emulator "mednafen", and set the NES emulator
  location in the TRSE settings dialog. 

  charset data and initial address are set in project settings

  you can edit the charset data / palette data using any .chr-editor such as YY-chr (https://www.romhacking.net/utilities/119/)


*/

@export "tiles/game_tiles.flf" "tiles/game_tiles.bin" 256

var  
   	i,j,k: byte; 
	// Must include dummy at| $8000 due to the fact we are using 2 16kb blocks. For a larger project,
	// this is where the music will go
	dummy: incbin("tiles/game_tiles.pal", $8000);
	palette: incbin("tiles/game_tiles.pal");
	



// NMI will automatically be called on every vblank. Update PPU gfx here! For now,
// it is emtpy. 

interrupt NMI();
begin
	startirq(0);
	closeirq();
end;


// Empty
interrupt IRQ();
begin

end;


/*
	Sets up the background screen by filling 32x30 rows with
    Afterwards, creating 64 random attribute (color) values and dumping them to the attribute 

*/

// use $0400 as temp storage area

@define storage $0400

procedure InitScreen();
begin
//	zp:=@storage;
	// fill 30 lines with some values
	ppupoint($24,00);
	for i:=0 to 30 do begin
		for j:=0 to 32 do
		begin
			ppuwrite(2+j&1 +(i&1)*16);
		end;
	end;
	// Fill nametable with : Palette 0 (00), 1(01), 2(10) and 3 (11)
	fill(^@storage,%00011110,64);
	// Dump 64 bytes from storage to PPU nametable 0 color ram ($2000 + $3C0);
	PPUAttributeDump(^@storage,$23+$4,$C0);
	
end;


// Starting point after resetting the NES
begin
	// Load palette
	LoadPalette(#palette);
	// Set up background & color values
	InitScreen();
	
	// Turn on background
	ToggleBackground(1);
	// Display background in border 0
	ToggleBorderBackground(1);

	// Press F1 on PPUCTRL for detailed info	
	togglebackgroundtableaddress(1);

	// set nametable 0 = $2000 (where we dumped the background data)
	SetNametable(1);
	// Turn on NMI
	ToggleNMI(1);
	// Halt! (this is where non-drawing gamelogic should happen)
	Loop();
end.
