Annotated MSX sources

Page 1/2
| 2

By slowerisbetter

Master (194)

slowerisbetter's picture

06-11-2011, 18:48

Any annoted MSX assembler sources? By that I mean games, applications, utilities set up in assembler with copious amounts of comments so people can learn from it?

Login or register to post comments

By slowerisbetter

Master (194)

slowerisbetter's picture

06-11-2011, 20:11

Ik lust wel assembly Smile

But yeah, I was just going to post I found PWND last week and that's what I mean. I found a lot of spectrum and atari 2600 stuff like that, even just commercial games disassembled and annotated, but MSX... Very sparsely...

I guess I'm waiting for this thread to be answered:

http://www.msx.org/forumtopic12598.html

How about a nice disassembler WIKI nice modern web 2.0 frontend which allows people to disassemble bins/roms allowing other people to annotate them :) Guess I would like it for myself even if no-one would use it :) What is currently the best disassembler which knows about MSX internals (aka, auto comment on all registers, ports, bios, vdp internals, interrupts etc)? It would not be hard to set this up I guess :)

By jltursan

Prophet (2519)

jltursan's picture

06-11-2011, 21:34

In my page, you can find a little 1K pong game assembler source with a lot of comments. If you feel brave and want any other source from my other programs you can ask for them freely. I haven't released yet because I think that they're are messy and extremely hard to follow :P
If you look for a nice collection of code snippets, you must visit the snippets thread from Karoshi's forum. There're a lot of good ideas there...

Anyway, a MSX Wiki is coming, isn't it?

By pitpan

Prophet (3138)

pitpan's picture

06-11-2011, 22:48

At Karoshi's forum there's also an opensource section: check it here!. There are four small games ready to go, with commented code and easy syntax.

By wolf_

Ambassador_ (9876)

wolf_'s picture

06-11-2011, 22:48

It is.. and everyone can contribute to it using their own account! And people don't even need to be skilled writers to start an article, as long as the information is correct, there'll always be someone around to polish the language.

By ro

Scribe (4414)

ro's picture

07-11-2011, 19:42

okay, here's the source of the Oracle replayer Smile
It includes some "oracle editor" routines references, but hey. you wanted it !

;  File:Orc-EPla.asm             project:Oracle  v1.10           update:22/08/97
;  Play an Oracle EDIT song/Module.                              (c) Fuzzy Logic
;  Fixed pitch down!                                             using: fK v2.21
; +-----------------------------------------------------------------------------+
	ORG	5*16+$F87F
	DM	"Orc-Epla.asm",0
	
SYS:	EQU	$0300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DRV
	JP	ORCINI
	JP	ORCRES
	JP	MAIN
	JP	SETVOL
ORCSTA:	DB	0
ORCADR:	DW	HDRADR
ORCMAP:	DS	5
ORCVOL:	DB	$3F,$3F	; Org,use
ORCPOS:	DB	0,0	; Cur,use
ORCPAT:	DB	0
ORCROW:	DB	-1,0	; Cur,nxt
ORCBYT:	DB	0	; User byte
CHIPS:	DB	00001111
PATADR:	DW	0	; Cur
	DB	0
ROWADR:	DW	0
ROWBUF:	DS	32*2	; Row queu
PLAMOD:	DB	0	; 0=Sng 1=Pat
	DW	SMPADR
MMIADR:	DS	2
MAIADR:	DS	2
DRIADR:	DS	2
ORCBAL:	DB	$3F,0,$3A,0,$3F,0,$27,0	; Org, use
OPLFRQ:	DS	2
PSGFRQ:	DS	2
SMPFRQ:	DS	2
CHNBUF:	DS	2
FILEID:	DB	0
.SPEED:	DB	0,0
.PSCAN:	DB	$80
	
RUN:	XOR	A
	EX	AF,AF
	CALL	$3B00
	RET	
	
; Init replayer
; In;  A, start pos
ORCINI:	LD	BC(ORCSTA)
	BIT	0,C
	RET	NZ
	
	LD	(ORCPOS)A
	LD	(ORCPOS+1)A
	
	CALL	IN_FE
	PUSH	AF
	LD	A,(ORCMAP)
	CALL	OUT_FE
	
	LD	HL,(CHNBUF)
	PUSH	HL
	POP	DE
	INC	DE
	LD	BC,$28*$20-1
	LD	(HL),$7F
	LDIR	
	
	XOR	A
	LD	(_GLTRN+1),A
	LD	A,(ORCVOL)
	CALL	SETVOL
	
	CALL	OUTINI
	LD	A,$C6
	LD	(O3RBNK+1),A
	LD	(O3ZBNK+1),A
	LD	A,3	; OPL mode
	LD	C,5
	CALL	MAFOUT
	
	LD	A,$C4
	LD	(O3RBNK+1),A
	LD	(O3ZBNK+1),A
	CALL	INISMP
	CALL	DRMINI
	CALL	PSGINI
	
; Copy part of header to replayer
	LD	HL,(ORCADR)
	LD	BC,|SETUP
	ADD	HL,BC
	LD	A,(HL)	; File ID
	INC	HL
	LD	(FILEID),A
	BI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-1
	CALL	MAFOUT
	DJNZ	SHUT.1
	
	LD	BC,$0800
SHUT.2:	LD	A,-1
	CALL	MMFOUT
	INC	C
	DJNZ	SHUT.2
	
	CALL	PSGINI
	CALL	DRMINI
	LD	C,$07	; smp
	LD	A,$01
	CALL	MAFOUT
	
	POP	AF
	CALL	OUT_FE
	RET	
	
; Set new MASTER volume
; In: A, vol
SETVOL:	LD	(ORCVOL+1),A
	LD	B,4
	LD	HL,ORCBAL
SETVL0:	LD	A,(ORCVOL+1)
	EX	AF,AF
	LD	A,(HL)
	INC	HL
	CALL	FIXVOL
	LD	(HL),A
	INC	HL
	DJNZ	SETVL0
	RET	
	
	
; Main routine
MAIN:	LD	A,(ORCSTA)
	AND	A
	RET	Z
	
; Handel 50Hz play
	LD	A,(FILEID)	; need to?
	BIT	4,A
	JP	Z,MAIN.A
	LD	A,(VDPBF2+9)	; cur. Hz
	BIT	1,A
	JP	NZ,MAIN.A
	LD	A,(FRQCNT)
	DEC	A
	LD	(FRQCNT),A
	JP	NZ,MAIN.A
	LD	A,6
	LD	(FRQCNT),A
	RET	
FRQCNT:	DB	6
	
MAIN.A:	CALL	IN_FE
	PUSH	AF
	LD	A,(ORCMAP)
	CALL	OUT_FE
	
	LD	A,(.PSCAN)
	AND	1
	CALL	Z,DO_EFX	; Handl FX
	
	LD	A,(.SPEED+1)
	DEC	A
	LD	(.SPEED+1),A
	CP	1
	JP	Z,SETROW	; prepare next row
	JR	NC,MAINEX
	LD	A,(.SPEED)
	LD	(.SPEED+1),A
	
; Do new row
	LD	A,(_GLTRN+1)
	LD	(_GLTRN),A
	
	CALL	NEWPRT
	LD	IY,(CHNBUF)
	LD	HL,ROWBUF
	LD	BC,(TOTCHN-1)
	XOR	A
MAIN.0:	LD	(CHANNL),A
	PUSH	BC
	PUSH	HL
	BIT	7,(IY)	; stat
	JR	Z,MAIN.3
	
	INC	HL
	CALL	EFFECT	; FX
	POP	HL
	PUSH	HL
	LD	A,(.PSCAN)
	AND	1
	CALL	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),A
	RET	
FXTYPE:	DB	0
	
	
; Set note
NOTE:	BIT	3,(IY+$17)	; lnk?
	JP	NZ,NOTE.0
	
	CALL	MAIN.2
	CALL	VELOCI
	LD	(IY+$03),A
	LD	A,(IY+$1A)	; Org bri
	LD	(IY+$02),A
	
NOTE.0:	LD	A,(HL)
	AND	$7F
	LD	(IY+$16),A
	
	PUSH	AF
	LD	HL,(OPLFRQ)
	CALL	GETFRQ
	BIT	0,(IY)
	CALL	NZ,MM_NTE
	BIT	1,(IY)
	CALL	NZ,MA_NTE
	SET	4,H	; Key ON
	LD	(IY+$04),L
	LD	(IY+$05),H
	POP	AF
	
	PUSH	AF
	BIT	2,(IY)
	CALL	NZ,SM_NTE
	POP	AF
	
	BIT	3,(IY)
	CALL	NZ,PS_NTE
;        SET   5,(IY+|ENVSU)
	RET	
	
VELOCI:	LD	A,R
	AND	$0F
	CP	(IY+$1B)
	JR	C,VELO.0
	LD	A,(IY+$1B)
VELO.0:	CPL	
	ADD	A,(IY+$19)	; Org_vol - velocity
	RET	C
	XOR	A
	RET	
	
	
	
MM_NTE:	BIT	3,(IY+$17)	; Lnk?
	JP	NZ,MMNTE0
	LD	A,(MMPORT)
	ADD	A,$10
	LD	C,A
	LD	A,H	; Key OFF
	CALL	MMFOUT
MMNTE0:	LD	A,(IY+$12)	; Rst bri, if org instr
	BIT	0,A
	JP	Z,MMNTE1
	PUSH	HL
	LD	L,(IY+$23)
	LD	H,(IY+$24)
	CALL	MMINP0
	LD	(IY+$12),A
	POP	HL
	RET	
MMNTE1:	AND	$F0
	RET	NZ
	LD	A,(MM.BRI)
	AND	$C0
	OR	(IY+$02)
	LD	(MM.BRI),A
	RET	
	
	
MA_NTE:	LD	A,(IY+$15)	; Rst bri
	AND	$C0
	OR	(IY+$02)
	LD	(IY+$15),A
	BIT	3,(IY+$17)	; Lnk?
	RET	NZ
	PUSH	HL
	LD	BC,(MAPORT)
	ADD	HL,HL
	DEC	HL
	SET	4,C
	LD	A,H	; Key OFF
	POP	HL
	JP	MAFOUT
	
SM_NTE:	CALL	GETSFR
	LD	(IY+$06),L
	LD	(IY+$07),H
	BIT	3,(IY+$17)	; Lnk?
	RET	NZ
SMNTE0:	LD	C,$07	; Key OFF
	LD	A,$01
	CALL	MAFOUT
	
	LD	HL,SMPADR
	LD	BC,$0409
SMNTE1:	LD	A,(HL)
	INC	HL
	CALL	MAFOUT
	INC	C
	DJNZ	SMNTE1
	
	LD	DE,(SMPADR+4)	; Loop adr
	LD	A,D
	AND	E
	CP	-1
	LD	C,$07
	LD	A,$A0	; (Key on)
	JP	Z,MAFOUT	;  no looping
	
	LD	HL,(SMPADR)
	ADD	HL,DE
	LD	A,$B0	; Key on, loop
	CALL	MAFOUT
	LD	C,$09	; Loop adr (re_start)
	LD	A,L
	CALL	MAFOUT
	INC	C
	LD	A,H
	JP	MAFOUT
	
PS_NTE:	LD	HL,(PSGFRQ)
	CALL	GETFRQ
	LD	(IY+$08),L
	LD	(IY+$09),H
	
	LD	A,(PSGPRT+2)	; Key on
	XOR	$3F
	OR	$C0
	LD	B,A
	LD	A,7
	OUT	($A0),A
	IN	A,($A2)
	AND	B
	LD	(PSGKEY),A
	LD	E,A
	LD	A,7
	JP	PSGOUT
	
; Note OFF
NTEOFF:	RES	4,(IY+$05)
;        RES   5,(IY+|ENVSU)
	CALL	MAIN.2
	BIT	2,(IY)
	CALL	NZ,SMPOFF
	BIT	3,(IY)
	JP	NZ,PSGOFF
	RET	
	
SMPOFF:	LD	C,$07
	LD	A,$01
	JP	MAFOUT
	
PSGOFF:; LD    A,(IY+|ENVST)
;        AND   A
;        RET   NZ
	LD	A,(PSGPRT+2)
	LD	B,A
	LD	A,7
	OUT	($A0),A
	IN	A,($A2)
	OR	B
	LD	(PSGKEY),A
	LD	E,A
	LD	A,7
	JP	PSGOUT
	
; Sustain OFF
NTESUS:	SET	5,(IY+$05)
	JP	NTEOFF
	
	
; Vibration
INIVIB:	BIT	4,(IY+$05)	; Key?
	RET	Z
	LD	A,(IY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D	BC,$20*$0A+1
	ADD	HL,BC
	BIT	1,(IY)
	CALL	NZ,MA_INS
	LD	A,(IY+$1A)
	LD	(IY+$02),A
	POP	HL
INSTR1:	BIT	2,(IY)
	JP	NZ,SM_INS
	RET	
	
MM_INS:	PUSH	HL
	LD	(IY+$20),0
	LD	A,(HL)	; nr
	INC	HL
	CALL	MMINS0	; set OPL
	POP	HL
	LD	(IY+$12),A
	RET	
	
MMINS0:	CP	$FD
	JR	C,MM_INO
	JP	Z,MM_INH
	JP	NC,MM_INP
	XOR	A
	RET	
MM_INO:	INC	HL	; Org.
	INC	HL
	LD	A,(HL)	; KSL+TL A (bri)
	LD	(MM.BRI),A
	AND	$3F
	LD	(IY+$1A),A
	DEC	HL
	DEC	HL
	LD	BC,$0800
MMINO0:	LD	A,(HL)
	INC	HL
	CALL	MMFOUT
	INC	C
	DJNZ	MMINO0
	XOR	A	; Instr.nr
	RET	
MM_INH:	LD	A,(HL)	; standard
	INC	HL
	AND	$F0
	RET	
MM_INP:	INC	HL	; St+
	LD	(IY+$23),L
	LD	(IY+$24),H
MMINP0:	LD	A,(HL)
	AND	$0F
	LD	(IY+$20),A
	LD	A,(HL)
	AND	$F0
	OR	1
	INC	HL
	LD	(IY+$21),L
	LD	(IY+$22),H
	RET	
	
MA_INS:	PUSH	HL
	PUSH	HL
	INC	HL
	INC	HL
	LD	A,(HL)	; KSL+TL A (bri)
	INC	HL
	LD	(IY+$15),A
	AND	$3F
	LD	(IY+$1A),A
	
	LD	A,(HL)	; KSL+TL B (vol)
	LD	(IY+$13),A
	POP	HL
	LD	DE,(MAPRT#)
	LD	B,8
	EX	DE,HL
MAINS0:	LD	C,(HL)	; port
	INC	HL
	LD	A,(DE)	; OPL
	INC	DE
	CALL	MAFOUT
	
	DJNZ	MAINS0
	LD	C,(HL)	; port
	INC	HL
	LD	A,(DE)	; OPL
	OR	$30
	CALL	MAFOUT
	POP	BC
	RET	
	
SM_INS:	LD	A,(IY+$01)	; Start & End adr
	ADD	A,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D	A,2
	LD	(FXTYPE),A
	LD	A,(IY+$0C)
	AND	$CF
	OR	$20
	LD	(IY+$0C),A
	
	LD	A,(HL)
	BIT	4,A
	JR	Z,BSLPOS
BSLNEG:	AND	$0F
	NEG	
	LD	(IY+$10),A
	RET	
BSLPOS:	AND	$0F
	LD	(IY+$10),A
	RET	
	
	
; Brightness
BRIGHT:	LD	A,(IY+$0C)
	AND	$CF
	LD	(IY+$0C),A
	
	LD	A,(HL)
	AND	$3F
	LD	(IY+$1A),A
	
BRGHT0:	LD	(IY+$02),A
	BIT	0,(IY)
	CALL	NZ,MM_BRI
	BIT	1,(IY)
	RET	Z
	
MA_BRI:	LD	A,(IY+$15)
	AND	$C0
	OR	(IY+$02)
	LD	(IY+$15),A
	RET	
	
MM_BRI:	LD	A,(IY+$12)	; org?
	AND	$F0
	RET	NZ
	LD	A,(MM.BRI)
	AND	$C0
	OR	(IY+$02)
	LD	(MM.BRI),A
	RET	
	
	
; Tempo
SPEED:	LD	A,(HL)
	BIT	5,A
	JP	NZ,TRANSP
	AND	$1F
	CP	2
	JR	NC,SPEED0
	LD	A,2
SPEED0:	LD	(.SPEED),A
	LD	(.SPEED+1),A
	RET	
	
; Global transpose
TRANSP:	LD	A,(HL)
	AND	$0F
	JR	Z,TRANS0
	BIT	4,(HL)
	JR	Z,TRANS0
	CPL	
TRANS0:	LD	(_GLTRN+1),A
	RET	
	
	
; Envelope
ENVELO:	LD	A,(HL)
	AND	$0F
	LD	(IY+$1B),A
	RET	
	
; Retrig
RETRIG:	LD	A,3
	LD	(FXTYPE),A
	LD	A,(IY+$0C)
	AND	$3F
	OR	$40
	LD	(IY+$0C),A
	LD	A,(HL)
	AND	$0F
	INC	A
	LD	(IY+$11),A
	LD	(IY+$18),A
	RET	
	
	
; Timr byt
TMRBYT:	LD	A,(HL)
	AND	$0F
	LD	(ORCBYT),A
	RET	
	
	
; Pat break
PATBRK:	LD	A,(TOTROW)
	LD	(ORCROW),A
	RET	
	
	
; Note link
NTELNK:	XOR	A
	LD	(FXTYPE),A
	LD	A,(IY+$0C)
	AND	$FC
	LD	(IY+$0C),A
	SET	3,(IY+$17)
	RET	
	
	
; Reset chnl
RESCHN:	PUSH	HL
	LD	A,(CHANNL)
	LD	HL,(CRTADR)
	ADD	A,A
	ADD	A,A
	LD	C,A
	LD	B,0
	ADD	HL,BC
	INC	HL
	
	LD	A,(HL)	; instr
	AND	$1F
	BIT	6,(IY)
	PUSH	AF
	CALL	Z,INSTR0
	POP	AF
	CALL	NZ,DRMINS
	INC	HL
	LD	A,(HL)
	CALL	TUNE.0
	INC	HL
	BIT	6,(IY)	; vol
	PUSH	AF
	CALL	Z,VOLUME
	POP	AF
	CALL	NZ,DRMVOL
	
	CALL	KILLFX
	BIT	6,(IY)
	CALL	Z,NTEOFF
	LD	(IY+|ENVST),0
	POP	HL
	RET	
	
	
; Clear envl
CLRENV:	LD	(IY+|ENVST),0
	LD	A,(IY+$19)
	LD	(IY+$03),A
	RET	
	
; Quit all FX
KILLFX:	LD	(IY+$0C),0
	RET	
	
	
; Drum channel:
DRUM:	AND	11000011
	RET	Z
	LD	A,(HL)
	AND	A
	RET	Z
	LD	(IY+$16),A
	BIT	0,(IY)
	CALL	NZ,MM_DRM
	BIT	1,(IY)
	JP	NZ,MA_DRM
	RET	
	
MM_DRM:	LD	C,$0E
	CALL	MMFOUT
	SET	5,A	; key on
	JP	MMFOUT
	
MA_DRM:	AND	$1F
	LD	C,$BD
	CALL	MAFOUT
	SET	5,A	; key on
	JP	MAFOUT
	
DRUMFX:	LD	A,(IY+$17)
	BIT	7,A
	RET	Z
	AND	$07
	CP	2
	JP	C,DRMVOL
	JP	Z,DRMINS
;        CP    4
;        JP    Z,VOLSLD
	CP	6
	JP	Z,SPEED
	CP	7
	JP	FX_2ND
	
DRMVOL:	CALL	VOLUME
	PUSH	HL
	LD	L,(IY+$04)
	LD	H,(IY+$05)
	LD	BC,6
	ADD	HL,BC
	EX	DE,HL
	BIT	0,(IY)
	PUSH	DE
	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D	L,(IY+$21)
	LD	H,(IY+$22)
	LD	A,(HL)
	AND	$F0
	OR	1
	LD	(IY+$12),A
	LD	A,(HL)
	AND	$0F
	INC	HL
	LD	(IY+$20),A
	LD	(IY+$21),L
	LD	(IY+$22),H
	RET	
	
	
; Frequency controller
DO_FRQ:	BIT	4,(IY+$05)	; key?
	RET	Z
	
	LD	A,(IY+$0C)
	AND	$03
	RET	Z	; normal
	CP	2
	JP	C,DO_VIB
	JP	Z,DO_PIT
	JP	DO_ARP
	
;  Handle VIBration
DO_VIB:	LD	A,(IY+$0E)
	INC	A
	CP	12
	JR	C,DOVIB0
	XOR	A
DOVIB0:	LD	(IY+$0E),A
	LD	HL,VIBTBL
	ADD	A,A
	LD	C,A
	LD	B,0
	ADD	HL,BC
	LD	C,(HL)
	INC	HL
	LD	B,(HL)
	LD	L,(IY+$04)
	LD	H,(IY+$05)
	ADD	HL,BC
	LD	E,0
	JP	ADFRQ0
VIBTBL:	DW	2,1,1,-1,-1,-2,-2,-1,-1,1,1,2
	
;  Handle Pitch bend
DO_PIT:	LD	C,(IY+$0D)
	LD	B,(IY+$0E)
	JP	ADDFRQ
	
;  Handle Arpeggio
DO_ARP:	CALL	DOARP1
	LD	HL,(OPLFRQ)
	CALL	GETFRQ
	SET	4,H
	LD	(IY+$04),L
	LD	(IY+$05),H
	CALL	GETSFR
	LD	(IY+$06),L
	LD	(IY+$07),H
	LD	HL,(PSGFRQ)
	CALL	GETFRQ
	LD	(IY+$08),L
	LD	(IY+$09),H
	RET	
	
DOARP1:	LD	A,(IY+$0E)
	CP	1
	JP	C,DARP10
	JR	Z,DARP11
	JP	DARP12
DARP10:	LD	(IY+$0E),1	; Base (1st half note)
	LD	A,(IY+$16)
	RET	
DARP11:	LD	(IY+$0E),2	; Add (2nd half note)
	LD	A,(IY+$0D)
	AND	7
	INC	A
	ADD	A,(IY+$16)
	INC	A
	RET	
DARP12:	LD	(IY+$0E),0	; Add (3rd half note)
	LD	A,(IY+$0D)
	PUSH	AF
	AND	7
	LD	C,A
	POP	AF
	RRCA	
	RRCA	
	RRCA	
	AND	7
	CP	C
	JP	Z,DARP10
	INC	A
	ADD	A,(IY+$16)
	INC	A
	RET	
	
	
; Volume controller
DO_VOL:	LD	A,(IY+$0C)
	AND	$0C
	RET	Z	; normal
	CP	$08
	JP	C,DO_TRM
	JP	Z,DO_VSL
	RET	
	
; Handle TReMolo
DO_TRM:	LD	A,(IY+$0F)
	INC	A
	CP	12
	JP	C,DOTRM0
	XOR	A
DOTRM0:	LD	(IY+$0F),A
	LD	HL,VIBTBL
	ADD	A,A
	LD	C,A
	LD	B,0
	ADD	HL,BC
	LD	A,(IY+$03)
	ADD	A,(HL)
	LD	(IY+$03),A
	RET	
	
; Handle Volume slide
DO_VSL:	LD	A,(.SPEED+1)
	AND	1
	RET	Z
	
	LD	A,(IY+$0F)
	BIT	7,A
	JR	NZ,DVSL.N
	ADD	A,(IY+$03)
	CP	$3F
	JR	C,DVSL0
	LD	A,$3F
DVSL0:	LD	(IY+$03),A
	LD	(IY+$19),A
	RET	
DVSL.N:	ADD	A,(IY+3)
	JR	C,DVSL0
	XOR	A
	JR	DVSL0
	
; Brightness controller
DO_BRI:	LD	A,(IY+$0C)
	AND	$30
	RET	Z	; normal
	CP	$20
	JP	C,DO_WAH
	JP	Z,DO_BSL
	RET	
	
; Handle WahWah
DO_WAH:	LD	A,(IY+$10)
	INC	A
	CP	10
	JR	C,DOWAH0
	XOR	A
DOWAH0:	LD	(IY+$10),A
	LD	HL,WAHTBL
	LD	C,A
	LD	B,0
	ADD	HL,BC
	LD	A,(IY+$02)
	ADD	A,(HL)
	AND	$3F
	JP	BRGHT0
WAHTBL:	DB	2,1,0,-1,-2,-2,-1,0,1,2
	
; Handle Bright slide
DO_BSL:	LD	A,(.SPEED+1)
	AND	1
	RET	Z
	
	LD	A,(IY+$10)
	BIT	7,A
	JR	NZ,DBSL.N
	ADD	A,(IY+$02)
	CP	$3F
	JR	C,DBSL0
	LD	A,$3F
DBSL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	C,$11
	CALL	MAFOUT
	LD	A,(IY+$07)
	DEC	C
	JP	MAFOUT
	
PSWFRQ:	LD	E,(IY+$08)
	LD	A,(PSGPRT)
	CALL	PSGOUT
	INC	A
	LD	E,(IY+$09)
	JP	PSGOUT
	
; Write Volume to outports
WR_VOL:	BIT	0,(IY)
	CALL	NZ,MMWVOL
	BIT	1,(IY)
	CALL	NZ,MAWVOL
	BIT	2,(IY)
	CALL	NZ,SMWVOL
	BIT	3,(IY)
	JP	NZ,PSWVOL
	RET	
	
MMWVOL:	LD	A,(ORCBAL+1)
	EX	AF,AF
	LD	A,(IY+$03)
	CALL	FIXVOL
	RRCA	
	RRCA	
	AND	$0F
	XOR	$0F
	LD	B,A
	LD	A,(IY+$12)	; get instr.
	AND	$F0
	OR	B
	LD	BC,(MMPORT+1)
	JP	MMFOUT
	
MAWVOL:	LD	A,(ORCBAL+3)
	EX	AF,AF
	LD	A,(IY+$03)
	CALL	FIXVOL
	XOR	$3F
	LD	B,A
	LD	A,(IY+$13)
	AND	$C0
	OR	B
;        LD    (IY+#13),A
	LD	HL,(MAPRT#)
	INC	HL
	INC	HL
	INC	HL
	LD	C,(HL)
	JP	MAFOUT
	
SMWVOL:	LD	A,(ORCBAL+5)
	EX	AF,AF
	LD	A,(IY+$03)
	CALL	FIXVOL
	RLCA	
	RLCA	
	AND	$FC
;        LD    (IY+#14),A
	LD	C,$12
	JP	MAFOUT
	
PSWVOL:	LD	A,(ORCBAL+7)
	EX	AF,AF
	LD	A,(IY+$03)
	CALL	FIXVOL
	RRCA	
	RRCA	
	AND	$0F
;        LD    (IY+#14),A
	LD	E,A
	LD	A,(PSGPRT+1)
	JP	PSGOUT
	
	
; Write Brightness to outports
WR_BRI:	BIT	0,(IY)
	CALL	NZ,MMWBRI
	BIT	1,(IY)
	CALL	NZ,MAWBRI
	BIT	3,(IY)
	JP	NZ,PSWBRI
	RET	
	
MMWBRI:	LD	A,(IY+$12)	; org instr.?
	AND	$F0
	RET	NZ
	LD	A,(MM.BRI)
	LD	C,2
	JP	MMFOUT
	
MAWBRI:	LD	A,(IY+$15)
	LD	HL,(MAPRT#)
	INC	HL
	INC	HL
	LD	C,(HL)
	JP	MAFOUT
	
PSWBRI:	LD	A,(IY+$02)
	RRCA	
	AND	$1F
	LD	E,A
	LD	A,6
	JP	PSGOUT
	
	
; Set row for next frame
SETROW:	CALL	NXTROW
	CALL	COPROW	; copy 'n decrn 2 buf
	LD	A,(ORCMAP)
	CALL	OUT_FE
	CALL	SET_FX	; Set FX nr in buf
	JP	MAINEX
	
NXTROW:	LD	A,(ORCROW)
	INC	A
	LD	(ORCROW),A
	LD	BC,(TOTROW)
	CP	C
	RET	C
	JP	NXTPOS
	
; Position up
NXTPOS:	LD	A,0
	LD	(ORCROW),A
	LD	A,(PLAMOD)
	CP	2
	LD	A,(ORCPOS)
	JP	Z,NXPOS0
	LD	A,(ORCPOS)
	LD	BC,(SNGLEN)
	CP	C
	JP	NC,ENDSNG
	INC	A
NXPOS0:	LD	(ORCPOS),A
	LD	HL,(PTBADR)
	LD	C,A
	LD	B,0
	ADD	HL,BC
	LD	A,(HL)
	LD	(ORCPAT),A
	CALL	GETPAT
	LD	(ROWADR),HL
	LD	(PATADR),HL
	LD	(PATADR+2),A
	XOR	A
	LD	(ORCROW),A
	RET	
ENDSNG:	LD	A,B
	CP	-1
	JP	Z,ORCRES
	JP	NXPOS0	; restart
	
; Copy row and decrn to buf
COPROW:	LD	A,(PATADR+2)	; Current map
	CALL	OUT_FE
	LD	HL,(ROWADR)
	LD	DE,ROWBUF
	LD	BC,(TOTCHN)
CROW.0:	LD	A,(HL)
	AND	$7F
	CP	$7C
	JR	NC,CROW.2	; decrun
	LD	A,(HL)
	INC	HL
	LD	(DE),A	; note
	INC	DE
	LDI		; FX
CROW.1:	LD	A,C
	AND	A
	JP	NZ,CROW.0
	LD	(ROWADR),HL
	RET	
CROW.2:	INC	HL
	SUB	$7C	; decrn
	LD	B,A
	XOR	A
CROW.3:	LD	(DE),A
	INC	DE
	LD	(DE),A
	INC	DE
	DEC	C
	DJNZ	CROW.3
	JR	CROW.1
	
; Set FX nr for next frame
SET_FX:	LD	HL,ROWBUF
	LD	IY,(CHNBUF)
	LD	DE,CBFLEN
	LD	BC,(TOTCHN-1)
SETFX0:	LD	A,(HL)
	INC	HL
	AND	$80
	RLCA	
	LD	A,(HL)
	RLA	
	RLA	
	RLA	
	AND	$07
	OR	$80
	LD	C,A
	CP	7	; Empty FX?
	JR	NZ,SETFX1
	LD	A,(HL)
	CP	$FF
	JR	NZ,SETFX1
	AND	7
SETFX1:	LD	(IY+$17),C
	INC	HL
	ADD	IY,DE
	DJNZ	SETFX0
	RET	
	
	
; Get Note Frequency
; In: HL, FRQ record
GETFRQ:	PUSH	AF
	LD	DE,(_GLTRN)	; Add global transpose
	ADD	A,E
	ADD	A,A
	LD	E,A
	LD	D,0
	ADD	HL,DE
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	LD	L,(IY+$0A)	; add channel tuning
	LD	H,(IY+$0B)
	ADD	HL,DE
	LD	A,H
	AND	$0F	; Key OFF!
	LD	H,A
	POP	AF
	RET	
	
; Get Frequency for ADPCM
GETSFR:	PUSH	AF
	LD	HL,(SMPFRQ)
	LD	DE,(_GLTRN)	; Add global transpose
	ADD	A,E
	ADD	A,A
	LD	E,A
	LD	D,0
	ADD	HL,DE
	LD	E,(HL)
	INC	HL
	LD	D,(HL)
	LD	L,(IY+$0A)	; add channel tuning
	LD	H,(IY+$0B)
	ADD	HL,DE
	LD	DE,(SMPADR+6)	; add sample tuning
	ADD	HL,DE
	POP	AF
	RET	
	
	
; In: BC
ADDFRQ:	LD	L,(IY+$04)	; MM frq
	LD	H,(IY+$05)
	LD	A,H
	AND	$01
	LD	E,A
	ADD	HL,BC
	
	BIT	7,B
	JR	Z,ADFRQ0
	LD	E,0
	BIT	0,H
	JR	NZ,ADFRQ0
	DEC	H
	
ADFRQ0:	LD	A,H
	AND	$1F
	OR	$10	; Key ON!
	OR	E
	LD	H,A
	LD	(IY+$04),L
	LD	(IY+$05),H
	
	LD	L,(IY+$06)	; ADPCM
	LD	H,(IY+$07)
	ADD	HL,BC
	LD	(IY+$06),L
	LD	(IY+$07),H
	
	LD	L,(IY+$08)	; PSG
	LD	H,(IY+$09)
	BIT	7,B
	JP	Z,ADFRQ1
	INC	BC
ADFRQ1:	SRL	B
	RR	C
	XOR	A
	SBC	HL,BC
	LD	(IY+$08),L
	LD	(IY+$09),H
	RET	
	
	
	
; Make relative volume
; In:  A, volume (00-3F). A', max vol.
; Out: A, new volume
FIXVOL:	AND	$3F
	XOR	$3F
	LD	C,A
	EX	AF,AF
	AND	$3F
	SUB	C
	RET	NC
	XOR	A
	RET	
	
	
; Init ports
NEWPRT:	PUSH	HL
	LD	HL,$0800
	LD	(PSGPRT),HL
	LD	A,00000001
	LD	(PSGPRT+2),A
	LD	HL,$3010
	LD	(MMPORT),HL
	LD	A,$A0
	LD	(MAPORT),A
	LD	HL,MA_PT0
	LD	(MAPRT#),HL
	LD	A,$C4
	LD	(O3RBNK+1),A
	LD	(O3ZBNK+1),A
	POP	HL
	RET	
	
; Channel UP
PS_CUP:	BIT	3,(IY)
	RET	Z
	LD	HL,(PSGPRT)
	LD	BC,$0102
	ADD	HL,BC
	LD	(PSGPRT),HL
	LD	A,(PSGPRT+2)
	RLCA	
	LD	(PSGPRT+2),A
	RET	
	
MM_CUP:	LD	A,(IY)
	BIT	0,A
	RET	Z
	BIT	6,A
	RET	NZ
	LD	A,(MMPORT)
	INC	A
	LD	(MMPORT),A
	ADD	A,$20
	LD	(MMPORT+1),A
	RET	
	
MA_CUP:	LD	A,(IY)
	BIT	1,A
	RET	Z
	BIT	6,A
	RET	NZ
	
	LD	HL,(MAPRT#)
	LD	BC,9
	ADD	HL,BC
	LD	(MAPRT#),HL
	LD	A,(MAPORT)
	INC	A
	LD	(MAPORT),A
	CP	$A9
	RET	C
	
	AND	$A0
	LD	(MAPORT),A
	LD	A,$C6
	LD	(O3RBNK+1),A
	L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BC
	RET	
RCOUNT:	DB	0
	
PSGOUT:	OUT	($A0),A
	PUSH	AF
	LD	A,E
	OUT	($A1),A
	POP	AF
	RET	

; 
	
PIB:;      Don't change label sequence!
TOTCHN:	DB	0
NTECHN:	DB	0
DRMCHN:	DB	0
TOTPAT:	DB	0
TOTROW:	DB	0
	DS	6
TOTOWI:	DB	0
TOTNTI:	DB	0
TOTDRI:	DB	0
TOTSMP:	DB	0
TOTENV:	DB	0
SNGLEN:	DB	0
RSTPOS:	DB	0
	
PTBADR:	DW	0
CRTADR:	DW	0
_GLTRN:	DB	0,0
CHANNL:	DB	0
	
MM.BRI:	DB	0	; MM, original Brightness (TL gen A)
PSGKEY:	DB	$3F
PSGPRT:	DB	$00,$08,$00	; Ports: FRQ, VOL, Chanl bit
MMPORT:	DB	$10,$30	;        FRQ, INS/VOL
MAPORT:	DB	$A0	;        FRQ
MAPRT#:	DW	MA_PT0
MA_PT0:	DB	$20,$23,$40,$43,$60,$63,$80,$83,$C0	; OPL regs
	DB	$21,$24,$41,$44,$61,$64,$81,$84,$C1
	DB	$22,$25,$42,$45,$62,$65,$82,$85,$C2
	DB	$28,$2B,$48,$4B,$68,$6B,$88,$8B,$C3
	DB	$29,$2C,$49,$4C,$69,$6C,$89,$8C,$C4
	DB	$2A,$2D,$4A,$4D,$6A,$6D,$8A,$8D,$C5
	DB	$30,$33,$50,$53,$70,$73,$90,$93,$C6
	DB	$31,$34,$51,$54,$71,$74,$91,$94,$C7
	DB	$32,$35,$52,$55,$72,$75,$92,$95,$C8
	
SMPADR:	DW	0,$1FFF,-1,0	; Current
	DS	32*8	; Buffer
	
LST:	
	
	


By hap

Paragon (2036)

hap's picture

07-11-2011, 20:21

Does it also mean you'd modify other people's source code for the sake of 'polish'?
Such as ro's typos and messy comments above (being a bit silly there Tongue but really, I wouldn't like mrc to adjust submitted sourcecode)

By ro

Scribe (4414)

ro's picture

07-11-2011, 21:17

typos? me? neeeeh Smile

By wolf_

Ambassador_ (9876)

wolf_'s picture

07-11-2011, 22:09

I doubt code has typos, or it wouldn't work.. unless you refer to typos in strings containing text. Even then, neh: I'm referring to the story around the subject, you don't have to be a Shakespeare to make an article.. just put down what you know and someone else will make it pleasant to read, and may even tweak the layout to make it look more fancy, or add categories to make it easier to find. No magic here, just Wiki!

By wolf_

Ambassador_ (9876)

wolf_'s picture

07-11-2011, 22:59

Besides, statistically the chance that the MRC changes articles is pretty slim if you compare the size of our crew with the amount of users in our database. Even when we wouldn't be touching source code, we can't guarantee that one of our regular users doesn't. That's the idea of a Wiki..

Page 1/2
| 2