Skip to content

Commit

Permalink
add system identification
Browse files Browse the repository at this point in the history
  • Loading branch information
decrazyo committed Jan 15, 2024
1 parent af027c2 commit d31693a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion include/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion kernel/bootstrap.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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 +
Expand Down
19 changes: 15 additions & 4 deletions kernel/nintendo/irqinit.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions kernel/nintendo/reset.s
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file modified lunix.fds
Binary file not shown.

0 comments on commit d31693a

Please sign in to comment.