TOCLEAN = common.img
TARGETS = pc.img bare.img floppy.img ahci.img
include ../common.mk

QEMU_FLAGS = -accel kvm -accel tcg \
	-chardev stdio,id=char0 \
	-serial chardev:char0 \
	-rtc base=utc
QEMU_DRVOPTS = format=raw,index=0,if=ide

common.img: $(DUSK)
	dd if=/dev/zero of=$@ bs=512 count=16384 # 8M
	$(DUSK) -n xcomp/i386/pc/mkfat -c '"$@" mountImage doit'
	$(DUSK) -c 'f<< xcomp/i386/pc/deploy.fs "$@" mountImage install'
	$(DUSK) -c '"$@"' -f $(DUSKDIR)/syncfs.fs

pc.img: common.img init.fs
	cp common.img $@
	$(DUSK) -f $(DUSKDIR)/fatcp.fs -c '"$@" mountImage cp init.fs' < init.fs

ata.img: common.img ata.fs
	cp common.img $@
	$(DUSK) -f $(DUSKDIR)/fatcp.fs -c '"$@" mountImage cp init.fs' < ata.fs

ahci.img: common.img ahci.fs
	cp common.img $@
	$(DUSK) -f $(DUSKDIR)/fatcp.fs -c '"$@" mountImage cp init.fs' < ahci.fs

bare.img: common.img bare.fs
	cp common.img $@
	$(DUSK) -f $(DUSKDIR)/fatcp.fs -c '"$@" mountImage cp init.fs' < bare.fs

floppy.img: $(DUSK) floppy.fs
	dd if=/dev/zero of=$@ bs=512 count=2880 # 1.44M
	$(DUSK) -n xcomp/i386/pc/mkfat -c '"$@" mountImage dofloppy'
	$(DUSK) -c 'f<< xcomp/i386/pc/deploy.fs "$@" mountImage install'
	$(DUSK) -n 'fs/fat xcomp/deploy' \
		-c '0 "$@" mountImage newfatfs copyall1440'
	$(DUSK) -f $(DUSKDIR)/fatcp.fs -c '"$@" mountImage cp init.fs' < floppy.fs

.PHONY: emul
emul: pc.img
	qemu-system-i386 $(QEMU_FLAGS) -drive file=pc.img,$(QEMU_DRVOPTS)

.PHONY: emulata
emulata: ata.img
	qemu-system-i386 $(QEMU_FLAGS) -drive file=ata.img,$(QEMU_DRVOPTS)

.PHONY: emulahci
emulahci: ahci.img
	qemu-system-i386 $(QEMU_FLAGS) -machine q35 \
		-drive file=ahci.img,$(QEMU_DRVOPTS)

.PHONY: emulbare
emulbare: bare.img
	qemu-system-i386 $(QEMU_FLAGS) -drive file=bare.img,$(QEMU_DRVOPTS)

.PHONY: emulfloppy
emulfloppy: floppy.img
	qemu-system-i386 $(QEMU_FLAGS) -fda floppy.img
