.PHONY: all rebuild clean spotless newimage

all: tos.sys

rebuild: clean all

run:
	qemu-system-x86_64 -vga std -smp 8 -m 256 -drive id=disk,file=bmfs.image,format=raw,if=none -device ahci,id=ahci -device ide-drive,drive=disk,bus=ahci.0 -name "LLL"

tos.sys: vm.bin pure64.sys
	dd if=/dev/zero of=kernel.bin bs=1M count=1
	dd if=vm.bin of=kernel.bin bs=512 conv=notrunc
	dd if=heap.bin of=kernel.bin seek=256 bs=512 conv=notrunc
	./bmfs bmfs.image write kernel.bin
	cat pure64.sys kernel64.sys > tos.sys
	dd if=tos.sys of=bmfs.image bs=512 seek=16 conv=notrunc

vm.bin: vm.o vm-loader.o heap.bin
	gcc -T vm.ld -o vm.bin -ffreestanding -O3 -nostdlib vm-loader.o vm.o -lgcc -mno-red-zone

vm.o: vm.c prescheme.h
	gcc -O3 -o vm.o -c vm.c -nostdlib -nostartfiles -nodefaultlibs -mno-red-zone -std=gnu99 -ffreestanding

vm.c: ev.scm ps-macros.scm packages.scm terminal.scm
	scheme48 -i ps-vm-compiler.image < compile-vm.scm

vm-loader.o: vm-loader.asm
	nasm -f elf64 vm-loader.asm -o vm-loader.o

heap.bin: heap-writer.scm ev.scm ev-runtime.scm ev-environment.scm
	csi -s heap-writer.scm

heap-debug.bin: heap-writer.scm ev.scm ev-runtime.scm ev-environment.scm
	csi -D debug -s heap-writer.scm

newimage:
	dd if=/dev/zero of=bmfs.image bs=1M count=8
	./bmfs bmfs.image format /force
	dd if=bmfs_mbr.sys of=bmfs.image bs=512 conv=notrunc
	./bmfs bmfs.image create kernel.bin 1
	make clean

clean:
	rm -f *.o *.so *.bin tos.sys heap-writer vm.c

spotless:
	rm -f bmfs.image
	make clean

