# disable kvm to debug triple faults
QEMU_BIN=##PLACEHOLDER_1##
GDB_BIN=##PLACEHOLDER_2##
QEMU_ACCELERATION_FLAG=##PLACEHOLDER_3##
QEMU_DEBUG_FLAG=#-d cpu_reset,int
QEMU_ARGS=-machine q35 -drive id=disk,file=build/boot.img,format=raw,if=none \
	-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 \
	-no-reboot -no-shutdown $(QEMU_ACCELERATION_FLAG) $(QEMU_DEBUG_FLAG)

CROSS_COMPILE=##PLACEHOLDER_4##
.PHONY: run
run: build
	$(QEMU_BIN) $(QEMU_ARGS) -display curses -S -s
.PHONY: srun
srun: build
	$(QEMU_BIN) $(QEMU_ARGS) -display none -S -s -serial mon:stdio
.PHONY: nativerun
nativerun: build
	$(QEMU_BIN) $(QEMU_ARGS) -display none -serial mon:stdio

.PHONY: prepare
prepare:
	cmake -Bbuild -DCMAKE_BUILD_TYPE=Debug $(CROSS_COMPILE)
	cp build/compile_commands.json .

.PHONY: reprepare
reprepare: clean prepare
	true

.PHONY: build
build:
	cmake --build build -j 6 --target boot.img

.PHONY: clean
clean:
	-rm -rf build
	-rm compile_commands.json

.PHONY: clean-all
clean-all: clean
	-rm Makefile

.PHONY: debug
debug:
	-$(GDB_BIN) --symbols=build/kernel.out --init-eval-command 'source pretty-print.py' --init-eval-command 'set pagination off' --init-eval-command 'set output-radix 16' --init-eval-command 'set print pretty on' --init-eval-command 'target remote:1234'
	-killall $(QEMU_BIN)

build/boot.vdi: build/boot.img
	-rm build/boot.vdi
	VBoxManage convertfromraw $< $@ --format VDI
