CC = gcc
#CFLAGS	= -Wall -O2 -c -nostdlib
CFLAGS	= -fno-common -Wall -O2 -c\
	  -finhibit-size-directive -fno-ident\
	  -fomit-frame-pointer -fcall-used-ebx
BOOT = boot
SECOND = second
TEST = test

all :${BOOT} ${SECOND}.bin ${BOOT}.bin startup.bin
test	:${TEST}.bin

${SECOND}.bin: ${SECOND}.o calc.o
	ld -T ${SECOND}.ls -o $@ ${SECOND}.o calc.o -Map ${SECOND}.map

${BOOT}: ${BOOT}.o
	ld -T ${BOOT}.ls -o $@ ${BOOT}.o -Map ${BOOT}.map

stage3.bin: stage3.o page.o vga.o idt.o
	ld -T stage3.ls -o $@ stage3.o page.o vga.o idt.o -Map stage3.map

startup.bin: startup.o
	ld -T startup.ls -o $@ startup.o -Map startup.map

init.o: idt.c idt.h
	${CC} ${CFLAGS} $<

stage3.o: stage3.S
	${CC} ${CFLAGS} $<

startup.o: startup.S
	${CC} ${CFLAGS} $<

page.o: page.c page.h vga.h
	${CC} ${CFLAGS} $<

vga.o: vga.c vga.h
	${CC} ${CFLAGS} $<

${SECOND}.o: ${SECOND}.S
	${CC} ${CFLAGS} $<

${BOOT}.o: ${BOOT}.S
	${CC} ${CFLAGS} $<

calc.o: calc.c realmode.h
	${CC} ${CFLAGS} $<

${BOOT}.bin:${BOOT} ${SECOND}.bin stage3.bin startup.bin
	dd if=/dev/zero of=${BOOT}.bin bs=512 count=2880
	dd if=${BOOT} of=${BOOT}.bin conv=notrunc
	dd if=${SECOND}.bin of=${BOOT}.bin bs=512 seek=1 conv=notrunc
	dd if=startup.bin of=${BOOT}.bin bs=512 seek=7 conv=notrunc
	dd if=stage3.bin of=${BOOT}.bin bs=512 seek=29 conv=notrunc
	dd if=end_file of=${BOOT}.bin bs=512 seek=2879 conv=notrunc
#	cp ${BOOT}.bin /dev/fd2

${TEST}.bin:${SECOND}.bin
	dd if=/dev/zero of=${TEST}.bin bs=512 count=2880
	dd if=${SECOND}.bin of=${TEST}.bin conv=notrunc
#	cp ${TEST}.bin /dev/fd1
	

clean:
	rm -f *.o *.map ${BOOT} ${SECOND}.bin ${SECOND}.o ${SECOND}.map *.bin
