From d31693ae76a9f8c881198539b369f08e0e3df906 Mon Sep 17 00:00:00 2001 From: decrazyo Date: Mon, 15 Jan 2024 04:38:18 -0600 Subject: [PATCH] add system identification --- Makefile | 6 ++++-- include/system.h | 4 +++- kernel/bootstrap.s | 25 ++++++++++++++++++++++++- kernel/nintendo/irqinit.s | 19 +++++++++++++++---- kernel/nintendo/reset.s | 36 ++++++++++++++++++++++++++++++++++++ lunix.fds | Bin 19561 -> 19648 bytes 6 files changed, 82 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 6225c89..2ec7f01 100644 --- a/Makefile +++ b/Makefile @@ -95,13 +95,15 @@ binaries: all -mkdir $(BINDIR) -cp kernel/boot.$(MACHINE) kernel/lunix.$(MACHINE) $(MODULES:%=kernel/modules/%) $(BINDIR) -nintendodisc: nintendopackage - mkfds -# -i -b 4 lunix.fds pkg/kyodaku.bin pkg/ascii.bin pkg/reset.bin pkg/boot.bin pkg/lunix.bin pkg/sh.bin pkg/ls.bin pkg/cat.bin pkg/pwd.bin pkg/ps.bin pkg/hello.bin +mesen: nintendodisc # mesen label files -rm ./lunix.mlb sed -nE 's/#define\s+(\w+)\s+([0-9]+).*/\2 \1/p' ./include/zp.h | xargs printf 'R:%x:%s\n' >> ./lunix.mlb sed -nE 's/#define\s+(\w+)\s+\$$(.*)/\1 \2/p' ./include/ksym.h | awk '{ $$2 = sprintf("%d","0x" $$2) - "0x6000"; printf("W:%x:%s\n", $$2, $$1) }' >> ./lunix.mlb + mono ~/.local/bin/Mesen.exe ./lunix.fds & +nintendodisc: nintendopackage + mkfds -# -i -b 4 lunix.fds pkg/kyodaku.bin pkg/ascii.bin pkg/reset.bin pkg/boot.bin pkg/lunix.bin pkg/sh.bin pkg/ls.bin pkg/cat.bin pkg/pwd.bin pkg/ps.bin pkg/hello.bin nintendopackage: binaries -mkdir pkg diff --git a/include/system.h b/include/system.h index 916ad4b..a19310a 100644 --- a/include/system.h +++ b/include/system.h @@ -140,10 +140,12 @@ #define lk_modroot SYSTEM_MEMORY+$ed ;// root of linked list of modules (16bit) #define lk_consmax SYSTEM_MEMORY+$ef ;// absolute number of consoles #define lk_archtype SYSTEM_MEMORY+$f0 ;// machine architecture -# define larchf_type %00000011 ;// type of machine +# define larchf_type %00000111 ;// type of machine # define larch_c64 0 # define larch_c128 1 # define larch_atari 2 +# define larch_nintendo 3 +# define larchf_2a0x %00001000 ;// flag for RP2A0x CPU (no decimal mode) # define larchf_8500 %00010000 ;// flag for 85xx (not 65xx) CPU # define larchf_pal %00100000 ;// flag for PAL (not NTSC) video hardware # define larchf_reu %01000000 ;// flag for available REU hardware diff --git a/kernel/bootstrap.s b/kernel/bootstrap.s index 2dfbae4..2db8166 100644 --- a/kernel/bootstrap.s +++ b/kernel/bootstrap.s @@ -402,6 +402,18 @@ noide64_txt: .text "Kernel panic: IDE64 not detected",$0a + #endif + ;; use decimal mode to detect Ricoh 2A0x CPU. + sed + lda #$09 + clc + adc #01 + cmp $10 + beq + + lda lk_archtype + ora #larchf_2a0x + sta lk_archtype + + cli + ;; spawn init task lda #0 sta userzp @@ -471,6 +483,8 @@ txt_c128: .text "Commodore 128",0 txt_atari: .text "Atari",0 +txt_nintendo: + .text "Famicom/NES",0 txt_pal: .text " (PAL)",0 txt_ntsc: @@ -489,12 +503,16 @@ print_machine_type: beq ++ cmp #larch_atari beq +++ - bne ++++ + cmp #larch_nintendo + beq ++++ + bne +++++ + ldy #txt_c64-txt_c64 SKIP_WORD + ldy #txt_c128-txt_c64 SKIP_WORD + ldy #txt_atari-txt_c64 + SKIP_WORD + + ldy #txt_nintendo-txt_c64 jsr mout + lda lk_archtype @@ -510,7 +528,12 @@ print_machine_type: bne mout ldy #txt_60hz-txt_c64 #else + ; assume that frequency matches the video standard. + lda lk_archtype ldy #txt_50hz-txt_c64 + and #larchf_pal + bne mout + ldy #txt_60hz-txt_c64 #endif mout: lda txt_c64,y beq + diff --git a/kernel/nintendo/irqinit.s b/kernel/nintendo/irqinit.s index ee6593d..4bbcd43 100644 --- a/kernel/nintendo/irqinit.s +++ b/kernel/nintendo/irqinit.s @@ -19,12 +19,23 @@ sta FDS_IRQ_CTRL ; configure the timer to generate an IRQ every 1/64 second. + lda lk_archtype + and #larchf_pal + bne + + ; NES/Famicom (NTSC) CPU @ 1.789773 MHz ; 1789773 hz / 64 = 27965.203125 - lda #<27965 - sta FDS_TIMER_LO - lda #>27965 - sta FDS_TIMER_HI + ldx #<27965 + ldy #>27965 + bne ++ + + ; NES (PAL) CPU @ 1.662607 MHz + ; 1789773 hz / 64 = 25978.234375 + + ldx #<25978 + ldy #>25978 + + + stx FDS_TIMER_LO + sty FDS_TIMER_HI ; enable timer IRQ and reload the timer after every interrupt. lda #FDS_TIMER_CTRL_E | FDS_TIMER_CTRL_R diff --git a/kernel/nintendo/reset.s b/kernel/nintendo/reset.s index 9a9ec46..9e9cf6d 100644 --- a/kernel/nintendo/reset.s +++ b/kernel/nintendo/reset.s @@ -19,7 +19,43 @@ vblankwait1: bpl vblankwait1 ; We now have about 30,000 cycles to burn before the PPU stabilizes. + ; we'll use it to identify our system. + ldy #0 vblankwait2: + inx + bne noincy + iny +noincy: bit PPU_STATUS bpl vblankwait2 + + ; because of a hardware oversight, we might have missed a vblank flag. + ; so we have to account for 1 or 2 vblanks. + + ; system | cycles per | cycles per | 1 vblank | 2 vblanks + ; | vblank | iteration | Y X | Y X + ;-------------+------------+------------+----------+---------- + ; NTSC FC/NES | 29780 | 12.005 | $09 $B1 | $13 $62 + ; PAL NES | 33247 | 12.005 | $0A $D2 | $15 $A4 + ; Dendy | 35464 | 12.005 | $0B $8A | $17 $14 + + ; check if we encountered 2 vblanks... + tya + cmp #$10 + bcc nodiv2 + lsr a ; if so, divide by 2. +nodiv2: + sec + sbc #9 + ; system | A + ;-------------+-- + ; NTSC FC/NES | 0 + ; PAL NES | 1 + ; Dendy | 2 + ; unknown | 3+ + beq + + lda #larchf_pal + + ora #larch_nintendo + ora lk_archtype + sta lk_archtype diff --git a/lunix.fds b/lunix.fds index 8236c194ac5b45122098d1cd2cdd1265c7241d7b..e95b7304e327d1e1c6d64f51676a558582896343 100644 GIT binary patch delta 359 zcmaDkgYm#j#tk`wOs1@p^8~dO7BIZHz<5H3NkQQIjFSQr7`-fBa(-Z1sldt1`{91? zhx?P~2`*;*I@w9cN_6dq`ybop|zp4v9)O>!y?uN z4GN;k3hbQ`KF25j6I#ygmYAEFoS&=j=NdeDiLi+c`$>imd?y({@SbG)z;lxM1NQ~K z1q?b1cy$&?>nu=Ic$oyWbAc|7p^=TB#q{6rwv{T=<%>_Df@H8PM^qwlkw#Y#tk`wO!rtO=Lu@F>M$t?{GPm1a53Y&$+1FK94l>m6&zOP_D)_Sq~rHJ zNg*XkVO8?lZ;dCJH*l=|aG$fG_rraKwB$t$d;1eVuqi}$zPRvpDWm3rlLsz<b(l{weBe6C_<{2z(+7?V+zS|V z7VzpUkXD$K1hip+qNc(HrUe>6-U5T2B9o&mvY?y2-+Q1k$d7o%Z_~jIs zDND