nes

My nes projects
Log | Files | Refs | README

square.asm (5169B)


      1 segment HEADER      ; NES 2.0 Header section
      2     db "NES", 0x1A  ; Identification string
      3     db 0x02         ; LSB of PGR-ROM size (32 KiB)
      4     db 0x01         ; LSB of CHR-ROM size (8 KiB)
      5     db 0x00         ; Flag 6 (Use the NROM mapper and horizontal mirroring)
      6     db 0x08         ; Flag 7 (Use the NROM mapper and the NES 2.0 file format)
      7     db 0x00         ; Use the NROM mapper and no submapper
      8     db 0x00         ; No need to increase the ROM sizes
      9     db 0x00         ; No PRG-RAM
     10     db 0x00         ; NO CHR-RAM
     11     db 0x00         ; 0 is for NTSC, 1 is for PAL
     12     db 0x00         ; No special PPU
     13 
     14 segment PGR-ROM
     15     ; Init
     16     db 0x78             ; sei   ; ignore IRQ interrupts
     17     ; db 0xD8             ; cld   ; clear decimal mode (not needed for the nes)
     18     ; Disable PPU NMI interrupts
     19     db 0xA9, 0x00       ; lda #0x00
     20     db 0x8D, 0x00, 0x20 ; sta 0x2000
     21     ; Disable the APU's DMC IRQ interrupts
     22     db 0x8D, 0x10, 0x40 ; sta 0x4010
     23     ; Initialize the stack at 0xFF
     24     db 0xA2, 0xFF       ; ldx #0xFF
     25     db 0x9A             ; txs
     26 
     27     ; Wait for 2 vertical blanks
     28     ; the 8th bit of 0x2002 is set when in a vblank
     29     db 0x2C, 0x02, 0x20 ; bit 0x2002
     30     db 0x10, 0xFB       ; bpl -5
     31     db 0x2C, 0x02, 0x20 ; bit 0x2002
     32     db 0x10, 0xFB       ; bpl -5
     33 
     34     ; Play a triangle wave (E5)
     35     db 0xA9, 0x40       ; lda #0x40
     36     db 0x8D, 0x17, 0x40 ; sta 0x4017
     37     db 0xA9, 0x04       ; lda #0x04
     38     db 0x8D, 0x15, 0x40 ; sta 0x4015
     39     db 0xA9, 0x81       ; lda #0x81
     40     db 0x8D, 0x08, 0x40 ; sta 0x4008
     41     db 0xA9, 0x55       ; lda #0x55     ; Wave frequency for an E5
     42     db 0x8D, 0x0A, 0x40 ; sta 0x400A
     43     db 0xA9, 0x00       ; lda #0x00
     44     db 0x8D, 0x0B, 0x40 ; sta 0x400B
     45 
     46     ; Prepare for vblank interrupts
     47     db 0xA9, 0b10010000 ; lda #0b10010000
     48     db 0x8D, 0x00, 0x20 ; sta 0x2000
     49 
     50     ; Make the default background palette gray
     51     db 0xA9, 0x3F       ; ldb #0x3F
     52     db 0x8D, 0x06, 0x20 ; sta 0x2006
     53     db 0xA9, 0x00       ; ldb #0x00
     54     db 0x8D, 0x06, 0x20 ; sta 0x2006
     55     db 0x8D, 0x07, 0x20 ; sta 0x2007
     56 
     57     ; Reset the address latch
     58     db 0x8D, 0x02, 0x20 ; sta 0x2002
     59     ; Set the colour of the square to black (0x0D)
     60     db 0xA9, 0x3F       ; ldb #0x3F
     61     db 0x8D, 0x06, 0x20 ; sta 0x2006
     62     db 0xA9, 0x11       ; ldb #0x11
     63     db 0x8D, 0x06, 0x20 ; sta 0x2006
     64     db 0xA9, 0x0D       ; ldb #0x0D     ; Colour for black
     65     db 0x8D, 0x07, 0x20 ; sta 0x2007
     66 
     67     ; Allow drawing sprites and tiles
     68     db 0xA9, 0b00011000 ; lda #0b00011000
     69     db 0x8D, 0x01, 0x20 ; sta 0x2001
     70 
     71     ; Set the initial x and y positions of the square
     72     db 0xA9, 0x80       ; lda #0x80
     73     db 0x8D, 0x01, 0x00 ; sta 0x0001
     74     db 0xA9, 0x80       ; lda #0x80
     75     db 0x8D, 0x02, 0x00 ; sta 0x0002
     76 
     77     ; OAM data
     78     db 0xA9, 0x80       ; lda #0x80     ; y position
     79     db 0x8D, 0x00, 0x02 ; sta 0x0200
     80     db 0xA9, 0x00       ; lda #0x00     ; Sprite number
     81     db 0x8D, 0x01, 0x02 ; sta 0x0201
     82     db 0xA9, 0x00       ; lda #0x00     ; Attributes (palette 0)
     83     db 0x8D, 0x02, 0x02 ; sta 0x0202
     84     db 0xA9, 0x80       ; lda #0x80     ; x position
     85     db 0x8D, 0x03, 0x02 ; sta 0x0203
     86 
     87     ; Allow interrupts
     88     db 0x58             ; cli
     89 
     90 ; loop:
     91     db 0x4C, 0x77, 0x80 ; jmp loop
     92 
     93 ; read_input:
     94     
     95     db 0xA2, 0x00       ; ldx #0x00
     96     ; Empty the memory which will store the controller inputs
     97     db 0x86, 0x00       ; stx 0x00
     98 
     99     ; Probe the controller
    100     db 0xA9, 0x01       ; lda #0x01
    101     db 0x8D, 0x16, 0x40 ; sta 0x4016
    102 
    103     db 0xA9, 0x00       ; lda #0x00
    104     db 0x8D, 0x16, 0x40 ; sta 0x4016
    105 
    106     ; Loop reading buttons
    107     db 0xAD, 0x16, 0x40 ; lda 0x4016
    108     db 0x4A             ; lsr a
    109     db 0x66, 0x00       ; ror 0x00
    110     db 0xE8             ; inx
    111     db 0xE0, 0x08       ; cpx #0x08
    112     db 0xD0, 0xF5       ; bne -11
    113 
    114     db 0x60
    115 
    116 ; vblank:
    117     ; Draw
    118     db 0xA9, 0x02       ; lda #0x02
    119     db 0x8D, 0x14, 0x40 ; sta 0x4014
    120 
    121     ; Read controller inputs
    122     db 0x20, 0x7A, 0x80 ; jsr read_input
    123 
    124     ; Move the square based on the inputs
    125     ; Up
    126     db 0xA9, 0x10       ; lda #0x10
    127     db 0x25, 0x00       ; and 0x00
    128     db 0xF0, 0x02       ; beq 0x02
    129 
    130     db 0xC6, 0x01       ; inc 0x01
    131 
    132     ; Down
    133     db 0xA9, 0x20       ; lda #0x20
    134     db 0x25, 0x00       ; and 0x00
    135     db 0xF0, 0x02       ; beq 0x02
    136 
    137     db 0xE6, 0x01       ; inc 0x01
    138 
    139     ; Left
    140     db 0xA9, 0x40       ; lda #0x40
    141     db 0x25, 0x00       ; and 0x00
    142     db 0xF0, 0x02       ; beq 0x02
    143 
    144     db 0xC6, 0x02       ; inc 0x02
    145 
    146     ; Right
    147     db 0xA9, 0x80       ; lda #0x80
    148     db 0x25, 0x00       ; and 0x00
    149     db 0xF0, 0x02       ; beq 0x02
    150 
    151     db 0xE6, 0x02       ; inc 0x02
    152 
    153     ; Write x and y positions to the OAM data
    154     db 0xA5, 0x01       ; lda 0x01
    155     db 0x8D, 0x00, 0x02 ; sta 0x0200    ; y pos
    156     db 0xA5, 0x02       ; lda 0x02
    157     db 0x8D, 0x03, 0x02 ; sta 0x0203    ; x pos
    158 
    159     db 0xA5, 0x02       ; lda 0x02
    160 
    161     db 0x40             ; rti
    162 
    163 ; Vectors
    164 segment VECTORS start=0x10+0x8000-0x06
    165     db 0x94, 0x80 ; NMI
    166     db 0x00, 0x80 ; RESET
    167     db 0x00, 0x00 ; IRQ
    168 
    169 ; Graphical area
    170 segment CHR-ROM start=0x10+0x8000
    171     ; The sprite for a box using colour 1
    172     times 8 db 0xFF
    173     times 8 db 0x00