~rabbits/spacetime

9c28a93bcad0fc0d46f9107ba011d1a30b64f01a — Devine Lu Linvega 4 years ago 615a5f4
First draft
4 files changed, 170 insertions(+), 96 deletions(-)

M src/cart.asm
M src/sprite.chr
M tools/lin6
M tools/lin6.c
M src/cart.asm => src/cart.asm +160 -86
@@ 18,7 18,6 @@ SPRADDR             .equ $2003
PPUSCROLL           .equ $2005 
PPUADDR             .equ $2006 
PPUDATA             .equ $2007 
	; APU
APUCH1VOL           .equ $4000 ; APU
APUCH1SWP           .equ $4001 
APUCH1FRQ           .equ $4002 


@@ 27,10 26,6 @@ APUCH2VOL           .equ $4004
APUCH2SWP           .equ $4005 
APUCH2FRQ           .equ $4006 
APUCH2LEN           .equ $4007 
APUCH3CNT           .equ $4008 
APUCH3SWP           .equ $4009 
APUCH3FRQ           .equ $400a 
APUCH3LEN           .equ $400b 
APUCH4VOL           .equ $400c 
APUCH4SWP           .equ $400d 
APUCH4FRQ           .equ $400e 


@@ 61,11 56,12 @@ next@input              .dsb 1
rate                    .dsb 1 
timer                   .dsb 1 
seed                    .dsb 1 
temp                    .dsb 1 
	.ende 

;; reset

RESET:                         ;
RESET:
	SEI                          ; disable IRQs
	CLD                          ; disable decimal mode
	LDX #$40


@@ 77,11 73,11 @@ RESET:                         ;
	STX PPUMASK                  ; disable rendering
	STX $4010                    ; disable DMC IRQs

Vblankwait1:                   ; First wait for vblank to make sure PPU is ready
Vblankwait1: ; First wait for vblank
	BIT PPUSTATUS
	BPL Vblankwait1

Clrmem:                        ;
Clrmem:
	LDA #$00
	STA $0000, x
	STA $0100, x


@@ 91,11 87,11 @@ Clrmem:                        ;
	STA $0600, x
	STA $0700, x
	LDA #$FE
	STA $0200, x                 ; move all sprites off screen
	STA $0200, x                 ; move sprites off screen
	INX 
	BNE Clrmem

Vblankwait2:                   ; Second wait for vblank, PPU is ready after this
Vblankwait2: ; Second wait for vblank
	BIT PPUSTATUS
	BPL Vblankwait2



@@ 132,23 128,26 @@ SetupAttributes:
SetupSequence:
	LDX #$00
@loop:
	LDA Sequence,x
	STA $10,x                    ; zeropage second row
	LDA $00                      ; set entire sequence to "+"
	STA $10,x                    ; store on zeropage second row
	INX 
	CPX #$10
	BNE @loop

SetupVariables:
	LDA #$08
	STA rate
	STA rate                     ; default rate = 08
	LDA #$00
	STA note                     ; default note = C
	LDA #$01
	STA note
	LDA #$03
	STA octave
	STA octave                   ; default octave = 2

SetupAudio:
	LDA #%00001011               ; ch1 + ch2 + ch4
	STA APUCTRL

SetupSprite:
	LDA #$88
	ADC timer
	STA $0200                    ; set tile.y pos
	LDA #$02
	STA $0201                    ; set tile.id


@@ 156,20 155,16 @@ SetupSprite:
	STA $0202                    ; set tile.attribute

EnableSprites:
	LDA #%10010000               ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
	LDA #%10010000               ; enableNMI sprites0 background1
	STA PPUCTRL
	LDA #%00011110               ; enable sprites, enable background, no clipping on left side
	LDA #%00011110               ; sprites background noclip
	STA PPUMASK
	LDA #$00                     ; No background scrolling
	STA PPUADDR
	STA PPUADDR
	STA PPUSCROLL
	STA PPUSCROLL

;; main phase

MAIN:
	LDA next@input
	; style1
	CMP BUTTON_LEFT
	BEQ MoveLeft
	CMP BUTTON_RIGHT


@@ 178,6 173,15 @@ MAIN:
	BEQ MoveUp
	CMP BUTTON_DOWN
	BEQ MoveDown
	; style2
	CMP BUTTON_SELECT
	BEQ ResetSequence
	CMP BUTTON_START
	BEQ MoveRight
	CMP BUTTON_A
	BEQ MoveUp
	CMP BUTTON_B
	BEQ MoveDown
	JMP MAIN                     ; jump back to Forever, infinite loop

;; General functions


@@ 187,8 191,7 @@ MoveLeft:
	LDA cursor
	CMP #$ff
	BNE @done                    ; when equal skip
	; wrap around
	LDA #$0f
	LDA #$0f                     ; wrap around
	STA cursor
@done:
	JMP MoveEnd


@@ 198,8 201,7 @@ MoveRight:
	LDA cursor
	CMP #$10
	BCC @done                    ; when greater skip
	; wrap around
	LDA #$00
	LDA #$00                     ; wrap around
	STA cursor
@done:
	JMP MoveEnd


@@ 208,9 210,8 @@ MoveDown:
	LDX cursor
	DEC $10,x
	LDA $10,x
	CMP #$ff
	BCC @done                    ; when greater skip
	; wrap around
	CMP #$ff                     ; when equal skip
	BNE @done                    ; wrap around
	LDA #$07
	STA $10,x
@done:


@@ 220,9 221,8 @@ MoveUp:
	LDX cursor
	INC $10,x
	LDA $10,x
	CMP #$08
	BCC @done                    ; when greater skip
	; wrap around
	CMP #$08                     ; when greater skip
	BCC @done                    ; wrap around
	LDA #$00
	STA $10,x
@done:


@@ 234,19 234,28 @@ MoveEnd:
	STA RedrawTimeline
	JMP MAIN

ResetSequence:
	LDX #$00
@loop:
	LDA $00                      ; set entire sequence to "+"
	STA $10,x                    ; store on zeropage second row
	INX 
	CPX #$10
	BNE @loop
	JMP MoveEnd

;; interface

RedrawTimeline:
	LDX #$00
@loop:                         ;
	LDA #$22                     ; v pos
@loop:
	LDA #$22                     ; sprite v pos
	STA PPUADDR
	LDA TimelinePositions, x     ; x pos
	TXA                          ; sprite x pos
	STA PPUADDR
	LDA #$10, x
	LDA #$10, x                  ; find selectionselection
	CLC 
	ADC #$10
	; selection
	CPX cursor
	BNE @continue
	CLC 


@@ 260,7 269,7 @@ RedrawTimeline:

RedrawInterface:
	LDA PPUSTATUS
	; RedrawSprite
@sprite:
	LDA position
	ASL 
	ASL 


@@ 268,79 277,107 @@ RedrawInterface:
	CLC 
	ADC #$40
	STA $0203                    ; set tile.x pos
	; RedrawPosition
	LDA position
@position:
	LDA timer
	CLC 
	ADC #$20
	LDX #$48
	LDX #$40
	LDY #$22
	JSR DrawAt
	; RedrawRate
@rate:
	LDA rate
	CLC 
	ADC #$20
	LDX #$49
	LDX #$41
	LDY #$22
	JSR DrawAt
	; RedrawOctave
@octave:
	LDA octave
	CLC 
	ADC #$20
	LDX #$4b
	LDX #$4e
	LDY #$22
	JSR DrawAt
	; RedrawNote
@note:
	LDA note
	CLC 
	ADC #$30
	LDX #$4a
	LDX #$4f
	LDY #$22
	JSR DrawAt
	RTS 

DrawAt:                        ; (a:id,x,y)
DrawAt: ; (a:id,x,y)
	STY PPUADDR
	STX PPUADDR
	STA PPUDATA
	RTS 

DoIncNote:
IncOct:
	INC octave                   ; increment octave
	LDA octave                   ; load octave
	CMP #$04                     ; check if max
	BCC @done
	LDA #$00                     ; reset octave
	STA octave
@done:
	RTS 

DecOct:
	DEC octave                   ; decrement octave
	LDA octave                   ; load octave
	CMP #$0ff                    ; check if min
	BCC @done
	LDA #$03                     ; reset octave
	STA octave
@done:
	RTS 

IncNote:
	INC note
	LDA note
	CMP #$0c
	BNE @done
	LDA #$00
	STA note
	LDA #$00                     ; when reached end
	STA note                     ; reset note
	JSR IncOct
@done:
	JMP DoOperationEnd
	RTS 

DoDecNote:
DecNote:
	DEC note
	LDA note
	CMP #$ff
	BNE @done
	LDA #$0b
	STA note
	JSR DecOct
@done:
	RTS 

;; operators

DoInc:
	JSR IncNote
	JMP DoOperationEnd

DoIncOctave:
	INC octave
	LDA octave
	CMP #$08
	BCC @done
DoDec:
	JSR DecNote
	JMP DoOperationEnd

DoTop:
	LDA #$00
	STA octave
	STA note
	JSR IncOct
	INC seed
@done:
	JMP DoOperationEnd

DoDecOctave:
	DEC octave
	LDA octave
	CMP #$ff
	BNE @done
	LDA #$08
	STA octave
DoBottom:
	LDA #$00
	STA note
	JSR DecOct
	DEC seed
@done:
	JMP DoOperationEnd



@@ 353,12 390,19 @@ DoRandomNote:
	BCS @loop
	STA note
	INC seed
	; snare
	LDA #<$3f8
	STA APUCH4LEN
	LDA #>$3f8
	STA APUCH4FRQ
	LDA #%11000100
	STA APUCH4VOL
	JMP DoOperationEnd

DoIncMetro:
	INC rate
	LDA rate
	CMP #$08
	CMP #$0c
	BCC @done
	LDA #$01
	STA rate


@@ 370,7 414,7 @@ DoDecMetro:
	LDA rate
	CMP #$00
	BNE @done
	LDA #$08
	LDA #$0c
	STA rate
@done:
	JMP DoOperationEnd


@@ 389,13 433,13 @@ DoOperation:
	LDX position
	LDA $10,x
	CMP #$00                     ;
	BEQ DoIncNote
	BEQ DoInc
	CMP #$01
	BEQ DoDecNote
	BEQ DoDec
	CMP #$02
	BEQ DoIncOctave
	BEQ DoTop
	CMP #$03
	BEQ DoDecOctave
	BEQ DoBottom
	CMP #$04
	BEQ DoRandomNote
	CMP #$05


@@ 405,12 449,39 @@ DoOperation:
	CMP #$07
	BEQ DoRandomPosition

DoOperationEnd:
DoOperationEnd: ; return from operations
	RTS 

DoSound:
	; calculate note position, store in x
	LDX octave                   ; get oct in x
	LDA Offset,x                 ; get note offset from oct in y
	CLC 
	ADC note
	TAY 
	LDA Notes,y                  ; get note0 with oct
	TAX 
	; ch1
	LDA #%10100001
	STA APUCH1VOL
	STX APUCH1SWP
	STX APUCH1FRQ
	LDA #%01101011
	SEC 
	SBC note
	STA APUCH1LEN
	; ch2
	STX APUCH2VOL
	LDA position
	STA APUCH2SWP
	STX APUCH2FRQ
	LDA #%01101011
	STX APUCH2LEN
	RTS 

;; nmi phase

NMI:                           ;
NMI: ;

ReadJoy:
	LDA #$01


@@ 424,7 495,7 @@ ReadJoy:
	ROL down@input
	BCC @loop

SaveJoy:                      
SaveJoy:
	LDA down@input
	CMP last@input
	BEQ @done


@@ 448,6 519,7 @@ RunTimer:
	LDA #$00
	STA timer                    ; reset timer
	JSR DoOperation
	JSR DoSound
	INC position
	; check if position has reached 16
	LDA position


@@ 461,8 533,9 @@ Cleanup:
	JSR RedrawTimeline
	JSR RedrawInterface
	LDA PPUSTATUS
	LDA #$00                     ; No background scrolling
	LDA #$c0                     ; Shift background to center
	STA PPUSCROLL
	LDA #$00
	STA PPUSCROLL
	RTI                          ; return from interrupt



@@ 470,18 543,19 @@ Cleanup:

	.org $E000

Palettes:                      ;
Palettes: ;
	.db $0F,$3B,$16,$30, $3B,$3B,$0F,$0F, $0F,$3B,$0F,$0F, $0F,$3B,$0F,$0F
	.db $0F,$10,$17,$07, $0F,$3B,$0F,$0F, $0F,$0F,$0F,$0F, $0F,$0F,$0F,$0F

TimelinePositions:
	.db $08,$09,$0a,$0b,$0c,$0d,$0e,$0f,$10,$11,$12,$13,$14,$15,$16,$17
Notes: ;
	.db $d2,$c9,$b3,$bd,$a9,$9f,$96,$8e,$86 ; oct 5
	.db $7e,$77,$70,$6a,$64,$5e,$59,$54,$4f,$4b,$46,$42 ; oct 4
	.db $3f,$3b,$38,$34,$31,$2f,$2c,$29,$27,$25,$23,$21 ; oct 3
	.db $1f,$1d,$1b,$1a,$18,$17,$15,$14,$13,$12,$11,$10 ; oct 2
	.db $d2,$c9,$b3

Sequence:
	; .db $00,$01,$05,$01,$02,$06,$03,$04,$00,$01,$05,$01,$02,$06,$03,$07
	; .db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
	.db $01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01
	; .db $04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04,$04
Offset:
	.db $24,$18,$0c,$00

;; Vectors


M src/sprite.chr => src/sprite.chr +0 -0
M tools/lin6 => tools/lin6 +0 -0
M tools/lin6.c => tools/lin6.c +10 -10
@@ 1,5 1,5 @@
/* Lin6
  Version 1.1
  Version 1.2
  https://wiki.xxiivv.com/lin6
*/



@@ 13,25 13,26 @@
  Label names must always be capitalized.
  Label names aalways end with :.
  Label names are always preceeded with a linebreak.
  Label comments are always padded to col32.
  Label comments are never padded.
  Directive names are always padded to col2.
  Directive comments are always padded to col32.
  Directive comments are never padded.
  Opcode names are always uppercase.
  Opcode names are always padded to col2.
  Opcode comments are always padded to col32.
  Inline comments are always padded to col2.
  Spacing comments are always preceeded and followed by with a linebreak.
  Spacing comments are always preceeded and followed with a linebreak.
*/

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define VERSION "1.1"
#define VERSION "1.2"
#define BUFLEN 512

typedef enum { false, true } bool;

char *OPCODES[] = {"ADC", "AND", "ASL", "BCC", "BCS", "BEQ", "BIT", "BMI",
                   "BNE", "BPL", "BRK", "BVC", "BVS", "CLC", "CLD", "CLI",
                   "CLV", "CMP", "CPX", "CPY", "DEC", "DEX", "DEY", "EOR",


@@ 285,7 286,6 @@ void lint(char *filepath, char *output, bool do_debug) {
        strcat(output, "\n");
      }
      if (has_comment(line)) {
        fillstr(key, 30, ' ');
        strcat(output, key);
        strcat(output, " ");
        strcat(output, comment);


@@ 400,9 400,9 @@ void lint(char *filepath, char *output, bool do_debug) {
  fclose(fr);
}

void show_version() { puts("Lin6 " VERSION); }
void show_version(void) { puts("Lin6 " VERSION); }

void show_help() {
void show_help(void) {
  show_version();
  puts("");
  puts("A tool to format 6502 assembly code.\n");


@@ 447,4 447,4 @@ int main(int argc, char *argv[]) {
    }
  }
  return 0;
}
}
\ No newline at end of file