Makefile 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. MAKEFLAGS += -R -r
  2. ASMDIRS := roms/asmsrc
  3. SUBDIRS := include test roms fatfs/source zlib $(ROMDIRS)
  4. CROSS = ../tools/gnu/bin/riscv32-unknown-elf-
  5. CC = $(CROSS)gcc
  6. LD = $(CROSS)ld
  7. OBJCOPY = $(CROSS)objcopy
  8. AR = $(CROSS)ar
  9. NM = $(CROSS)nm
  10. PERL = perl
  11. GZIP = gzip
  12. INCLUDE = -I. -I./include -I../common -I./zlib -I./fatfs/source
  13. include ../riscv-opts.mk
  14. CPPFLAGS = $(INCLUDE) $(riscv_flags) -DON_FPGA
  15. CFLAGS = $(CPPFLAGS) -W -Wextra
  16. SFLAGS = $(CPPFLAGS) -D__ASSEMBLY__
  17. LDFLAGS = $(CFLAGS) \
  18. -Wl,-Map=$*.map \
  19. -Wl,--gc-sections \
  20. -Wl,--sort-section=alignment \
  21. -Wl,-z,common-page-size=16 \
  22. -Wl,-z,max-page-size=16 \
  23. -Wl,-z muldefs \
  24. -Wl,--require-defined=IODEV_BASE
  25. gendeps = -MD -MF $(@D)/.$(@F).d -MT $@
  26. VPATH := .:../common
  27. # Delete output files on error
  28. .DELETE_ON_ERROR:
  29. # Don't delete intermediate files
  30. .SECONDARY:
  31. genhdrs = iodevs.h irqtable.h roms.h
  32. gensrcs = ioregsa.S
  33. all: sram.bin dram.bin dram.hex checksum.h
  34. LIBS = max80.a roms.a fatfs.a zlib.a
  35. ROMS = $(shell find roms -name '*.rom' -print)
  36. ROMOBJ = $(ROMS:.rom=.o)
  37. FORCEOBJ = head.o dummy.o die.o system.o killed.o
  38. COMMONOBJ := $(patsubst ../common/%.c,%.o,$(wildcard ../common/*.c))
  39. LIBOBJ = debug.o ioregsa.o irqasm.o irqtable.o spurious_irq.o \
  40. console.o rtc.o romcopy.o spiflash.o esp.o matchver.o \
  41. config.o shutdown.o \
  42. sdcard.o \
  43. abcmem.o abcio.o abcdisk.o abcrtc.o abcpun80.o \
  44. memset.o memcpy.o \
  45. runtest.o start_test.o \
  46. $(COMMONOBJ)
  47. max80.a: $(LIBOBJ)
  48. rm -f $@
  49. $(AR) cq $@ $(LIBOBJ)
  50. ROMDEPS = roms
  51. roms.a: $(ROMDEPS)
  52. rm -f $@
  53. $(AR) cq $@ $(ROMOBJ)
  54. FATFS_C = $(wildcard fatfs/source/*.c)
  55. FATFS_O = $(FATFS_C:.c=.o)
  56. fatfs.a: $(FATFS_O)
  57. rm -f $@
  58. $(AR) cq $@ $(FATFS_O)
  59. ZLIB_C = $(wildcard zlib/*.c)
  60. ZLIB_O = $(ZLIB_C:.c=.o)
  61. zlib.a: $(ZLIB_O)
  62. rm -f $@
  63. $(AR) cq $@ $(ZLIB_O)
  64. CFLAGS_zlib/inflate.c := -Wno-implicit-fallthrough
  65. CFLAGS_zlib/infback.c := -Wno-implicit-fallthrough
  66. CFLAGS_memset.c := -O2
  67. %.hex: %.elf
  68. $(OBJCOPY) -O ihex $< $@
  69. %.mem: %.bin
  70. $(BIN2MEM) $< > $@
  71. sram.bin: max80.elf
  72. $(OBJCOPY) -O binary -R '.dram*' $< $@
  73. dram.bin: max80.elf
  74. $(OBJCOPY) -O binary -j '.dram*' $< $@
  75. %.bin: %.elf
  76. $(OBJCOPY) -O binary $< $@
  77. %.gz: %
  78. $(GZIP) -9 < $< > $@
  79. checksum.h: dram.bin sram.bin checksum.pl
  80. $(PERL) checksum.pl -o $@ -p sram.bin \
  81. -l $$(($$($(NM) -n max80.elf --radix=decimal | \
  82. grep ' [A-Z] __dram_init_end$$' | \
  83. awk '{ print $$1; }') - (1 << 30))) \
  84. dram.bin
  85. testimg.bin: testimg.elf
  86. $(OBJCOPY) -O binary $< $@
  87. testimg.o: testimg.S testimg.bin
  88. %.hex: %.bin
  89. $(OBJCOPY) -I binary -O ihex $< $@
  90. # Objects specific to certain executables
  91. max80.elf: diskcache.o
  92. jtagupd.elf: sbrk.o
  93. %.elf: %.ild $(FORCEOBJ) %.o $(LIBS)
  94. $(CC) $(LDFLAGS) -Wl,-T,$< -o $@ \
  95. -Wl,--start-group $(filter-out $<,$^) -Wl,--end-group
  96. %.o: %.c $(genhdrs)
  97. $(CC) $(CFLAGS) $(CFLAGS_$<) $(gendeps) -c -o $@ $<
  98. %.s: %.c $(genhdrs)
  99. $(CC) $(CFLAGS) $(CFLAGS_$<) $(gendeps) -S -o $@ $<
  100. %.i: %.c $(genhdrs)
  101. $(CC) $(CFLAGS) $(CFLAGS_$<) $(gendeps) -E -o $@ $<
  102. %.o: %.S $(genhdrs)
  103. $(CC) $(SFLAGS) $(SFLAGS_$<) -Wa,-ahlsm=$*.lst $(gendeps) -c -o $@ $<
  104. %.s: %.S $(genhdrs)
  105. $(CC) $(SFLAGS) $(SFLAGS_$<) -Wa,-ahlsm=$*.lst $(gendeps) -E -o $@ $<
  106. ioregsa.S: ioregs.h ioregsa.pl $(genhdrs)
  107. $(PERL) ioregsa.pl $< $@
  108. .PHONY: roms
  109. roms: asmsrc
  110. $(MAKE) _roms
  111. .PHONY: _roms
  112. _roms: $(ROMOBJ)
  113. roms/%.o: roms/%.rom rom.S
  114. $(CC) $(SFLAGS) $(SFLAGS_$(F<)) \
  115. -DNAME=rom_$$(echo '$*' | sed -r -e 's/[^A-Za-z0-9]+/_/g') \
  116. -DFILE='"$<"' -c -o $@ rom.S
  117. .PHONY: asmsrc
  118. asmsrc:
  119. for d in $(ASMDIRS); do $(MAKE) -C $$d; done
  120. roms.h: roms
  121. ( for r in $(ROMS); do \
  122. cn=$$(echo "$$r" | sed -r -e 's/^roms/rom_/' -e 's/\.rom$$//' \
  123. -e 's/[^A-Za-z0-9]+/_/g') ; \
  124. stat -c "extern const char $$cn[%s];" "$$r"; \
  125. done ) > $@
  126. abcmem.o: roms.h
  127. %.ild: %.ld $(genhdrs)
  128. $(CC) $(CFLAGS) $(CFLAGS_$<) $(gendeps) \
  129. -x assembler-with-cpp \
  130. -fdollars-in-identifiers \
  131. -C -P -E $< | $(PERL) -pe 's:^(#.*)$$:/* $$1 */:' > $@
  132. iodevs.h: ../common/iodevs.conf ../tools/iodevs.pl
  133. $(PERL) ../tools/iodevs.pl h $< $@
  134. irqtable.h: ../common/iodevs.conf ../tools/iodevs.pl
  135. $(PERL) ../tools/iodevs.pl irqh $< $@
  136. clean:
  137. for d in . $(shell find $(SUBDIRS) -type d); do \
  138. rm -f $$d/*.a $$d/*.o $$d/*.i $$d/*.s $$d/*.elf $$d/*.bin \
  139. $$d/.*.d $$d/*.ild $$d/*.map $$d/*.lst; \
  140. done
  141. rm -f $(genhdrs) $(gensrcs) $(ROMOBJ)
  142. spotless: clean
  143. for d in $(ASMDIRS); do $(MAKE) -C $$d $@; done
  144. rm -f *.mem *.hex *.mif checksum.h
  145. -include $(patsubst %,%/.*.d,. $(SUBDIRS))