all: stage2.bin disk.dd grub.iso

stage2.bin: stage2.asm
	fasm stage2.asm stage2.bin

disk.dd: boot.bin stage2.bin
	@dd if=/dev/zero of=empty bs=1024 count=1023
	@cat boot.bin stage2.bin empty >disk.dd
	@rm empty

grub.iso: stage2.bin
	@mkdir iso iso/bootboot iso/boot iso/boot/grub 2>/dev/null || true
	@cp stage2.bin iso/bootboot/loader || true
	@echo -e "screen=800x600\nkernel=sys/core\n" >iso/bootboot/config || true
	@cp initrd.bin iso/bootboot/initrd || true
	@echo -e "menuentry \"Multiboot test\" {\n  multiboot /bootboot/loader\n  module /bootboot/initrd\n  module /bootboot/config\n  boot\n}" >iso/boot/grub/grub.cfg || true
	grub-mkrescue -o grub.iso iso
	@rm -r iso 2>/dev/null || true

qemurom: stage2.bin
	qemu-system-x86_64 -option-rom stage2.bin -d int -serial stdio

qemubios: disk.dd
	qemu-system-x86_64 -drive file=disk.dd,format=raw -d int -serial stdio

qemugrub: grub.iso
	qemu-system-x86_64 -cdrom grub.iso -d int -serial stdio

bochsrom: stage2.bin
	bochs -f bochsrom.rc -q

bochsbios: disk.dd
	bochs -f bochsbios.rc -q

bochsgrub: grub.iso
	bochs -f bochsgrub.rc -q

clean:
	@rm stage2.bin disk.dd grub.iso com1.txt 2>/dev/null || true
