It is currently Sat Sep 21, 2019 7:02 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Cant Draw Background
PostPosted: Thu Mar 28, 2019 9:05 pm 
Offline

Joined: Mon Mar 25, 2019 10:58 am
Posts: 13
I'm stumped on how to draw nametables right now. I've tried many codes by other people and so far i haven't a clue on how drawing background nametables work. Whenever i run any variety of background writing code it always comes up as the first sprite in the sprite table (FYI I'm following N.N.'s tutorial so the tileset is mario sprites)

Here's the code.
Code:
  .inesprg 1   ; 1x 16KB PRG code
  .ineschr 1   ; 1x  8KB CHR data
  .inesmap 0   ; mapper 0 = NROM, no bank swapping
  .inesmir 1   ; background mirroring

;;;;;;;;;;;;;;;;

   .rsset $0000

gamestate      .rs 1
ballx         .rs 1
bally         .rs 1
ballup         .rs 1
balldown      .rs 1
ballleft      .rs 1
ballright      .rs 1
ballspeedx      .rs 1
ballspeedy      .rs 1
paddletop1      .rs 1
pattlebot1      .rs 1
paddletop2      .rs 1
pattlebot2      .rs 1
buttons1      .rs 1
buttons2      .rs 1
score1         .rs 1
score2         .rs 1
background      .rs 1
;;;;;;;;;;;;;;;;
;;Constants
STATETITLE         =      $00  ; displaying title screen
STATEPLAYING      =      $01  ; move paddles/ball, check for collisions
STATEGAMEOVER      =      $02  ; displaying game over screen

RIGHTWALL         =      $F4  ; when ball reaches one of these, do something
TOPWALL            =      $20
BOTTOMWALL         =      $E0
LEFTWALL         =      $04
 
PADDLE1X         =      $08  ; horizontal position for paddles, doesnt move
PADDLE2X         =      $F0

PAD_A            =      %10000000
PAD_B            =      %01000000
PAD_SEL            =      %00100000
PAD_STA            =      %00010000
PAD_UP            =      %00001000
PAD_DOWN         =      %00000100
PAD_LEFT         =      %00000010
PAD_RIGHT         =      %00000001
;;;;;;;;;;;;;;;;
   
   
   
   .bank 0
   .org $C000


RESET:
   SEI          ; disable IRQs
   CLD          ; disable decimal mode
   LDX #$40
   STX $4017    ; disable APU frame IRQ
   LDX #$FF
   TXS          ; Set up stack
   INX          ; now X = 0
   STX $2000    ; disable NMI
   STX $2001    ; disable rendering
   STX $4010    ; disable DMC IRQs
   JSR vblankwait  ;jump to vblank wait #1
clrmem:
   LDA #$00
   STA $0000, x
   STA $0100, x
   STA $0300, x
   STA $0400, x
   STA $0500, x
   STA $0600, x
   STA $0700, x
   LDA #$FE
   STA $0200, x
   INX
   BNE clrmem
   JSR vblankwait   ;jump to vblank wait again
   JSR LoadBackground
   JSR LoadPalettes

;;;Set some initial ball stats
  LDA #$01
  STA balldown
  STA ballright
  LDA #$00
  STA ballup
  STA ballleft
 
  LDA #$50
  STA bally
 
  LDA #$80
  STA ballx
 
  LDA #$02
  STA ballspeedx
  STA ballspeedy


;;:Set starting game state
  LDA #STATETITLE
  STA gamestate

  LDA #%10010000   ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
  STA $2000

  LDA #%00011110   ; enable sprites, enable background, no clipping on left side
  STA $2001

Forever:
   JMP Forever

vblankwait:      ;wait for vblank
   BIT $2002
   BPL vblankwait
   RTS

LoadBackground:
  LDA $2002             ; read PPU status to reset the high/low latch
  LDA #$20
  STA $2006             ; write the high byte of $2000 address
  LDA #$00
  STA $2006             ; write the low byte of $2000 address
  LDY #$00
  LDX #$00              ; start out at 0

Background
   LDX #$00
   LoadBackgroundLoop:
   LDA title, x   ; load data from address (background + the value in x)
   STA $2007             ; write to PPU
   INX                   ; X = X + 1
   CPX #$80              ; Compare X to hex $80, decimal 128 - copying 128 bytes
   BNE LoadBackgroundLoop  ; Branch to LoadBackgroundLoop if compare was Not Equal to zero
                        ; if compare was equal to 128, keep going down
   RTS

LoadPalettes:
  LDA $2002             ; read PPU status to reset the high/low latch
  LDA #$3F
  STA $2006             ; write the high byte of $3F00 address
  LDA #$00
  STA $2006             ; write the low byte of $3F00 address
  LDX #$00              ; start out at 0
LoadPalettesLoop:
  LDA palette, x        ; load data from address (palette + the value in x)
                          ; 1st time through loop it will load palette+0
                          ; 2nd time through loop it will load palette+1
                          ; 3rd time through loop it will load palette+2
                          ; etc
  STA $2007             ; write to PPU
  INX                   ; X = X + 1
  CPX #$20              ; Compare X to hex $10, decimal 16 - copying 16 bytes = 4 sprites
  BNE LoadPalettesLoop
  RTS
      

NMI:

;;Graphic Updates
   LDA #$00
   STA $2003
   LDA #$02
   STA $4014

   JSR DrawScore

   LDA #%10010000   ; enable NMI, sprites from Pattern Table 0, background from Pattern Table 1
   STA $2000
   LDA #%00011110   ; enable sprites, enable background, no clipping on left side
   STA $2001
   LDA #$00        ;;tell the ppu there is no background scrolling
   STA $2005
   STA $2005
    JSR UpdateBackgrounds

;; Gamecode
   JSR ReadController1  ;;get the current button data for player 1
   JSR ReadController2  ;;get the current button data for player 2

GameEngine:
   LDA gamestate
   CMP #STATETITLE
   BEQ GameTitle

   LDA gamestate
   CMP #STATEGAMEOVER
   BEQ GameOver

   LDA gamestate
   CMP #STATEPLAYING
   BEQ GamePlaying
GameEngineDone:
   JSR UpdateSprites
   JSR UpdateBackgrounds
   RTI

;;;;;;;;;;;;;;

GameTitle:
   LDA buttons1
   AND #PAD_STA
   BNE TitleButtonChk
   CLC
   LDA #STATEPLAYING
   STA gamestate
TitleButtonChk
   RTS

GameOver:
   LDA buttons1
   AND #PAD_STA
   BNE OverButtonChk
   CLC
   LDA #STATETITLE
   STA gamestate
OverButtonChk   
   RTS

GamePlaying:
   ;JSR Gameplay
   ;JSR DrawScore
   RTS
   

UpdateSprites:
  LDA bally  ;;update all ball sprite info
  STA $0200
 
  LDA #$30
  STA $0201
 
  LDA #$00
  STA $0202
 
  LDA ballx
  STA $0203
 
  ;;update paddle sprites
  RTS
 
UpdateBackgrounds:
   
   LDA gamestate
   CMP #STATETITLE
   BEQ DisplayTitle
   
   LDA gamestate
   CMP #STATEGAMEOVER
   BEQ DisplayOver

   LDA gamestate
   CMP #STATEPLAYING
   BEQ DisplayPlaying

   RTS

DisplayTitle:
   LDA title
   STA background
   RTS

DisplayOver:
   LDA over
   STA background
   RTS

DisplayPlaying:
   LDA game
   STA background
   RTS

DrawScore:
  ;;draw score on screen using background tiles
  ;;or using many sprites
  RTS

;;;;;;;;;;;;;;;;

;;Controller inputs
ReadController1:
  LDA #$01
  STA $4016
  LDA #$00
  STA $4016
  LDX #$08
ReadController1Loop:
  LDA $4016
  LSR A           ; bit0 -> Carry
  ROL buttons1     ; bit0 <- Carry
  DEX
  BNE ReadController1Loop
  RTS

ReadController2:
  LDA #$01
  STA $4016
  LDA #$00
  STA $4016
  LDX #$08
ReadController2Loop:
  LDA $4017
  LSR A           ; bit0 -> Carry
  ROL buttons2     ; bit0 <- Carry
  DEX
  BNE ReadController2Loop
  RTS

;;;;;;;;;;;;;;;;

 .bank 1                       ; change to bank 1
  .org $2000                    ; start at $E000

title:
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$44,$48,$48,$48,$48,$48,$48,$48,$48,$48,$48,$48,$48,$48,$48,$49,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$d0,$d1,$26,$d0,$d1,$26,$26,$d0,$d1,$26,$d0,$d1,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$db,$d5,$26,$db,$db,$26,$26,$db,$db,$26,$db,$26,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$db,$d7,$26,$db,$db,$26,$26,$db,$db,$26,$db,$db,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$db,$26,$26,$d4,$d5,$26,$26,$db,$db,$26,$d4,$d5,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$e1,$26,$26,$d6,$d7,$26,$26,$e1,$e1,$26,$d6,$d7,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$5f,$78,$78,$78,$78,$78,$78,$78,$78,$78,$78,$78,$78,$78,$78,$7a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$0c,$18,$19,$22,$20,$1b,$12,$1d,$0e,$24,$02,$00,$01,$09,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .org $2400
over:
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$d0,$d1,$26,$d0,$d1,$26,$d0,$e8,$d1,$26,$d0,$d1,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$db,$26,$26,$db,$db,$26,$db,$db,$db,$26,$e0,$26,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$db,$db,$26,$e7,$db,$26,$db,$26,$db,$26,$e0,$dc,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$d4,$d5,$26,$db,$db,$26,$db,$26,$db,$26,$d4,$d5,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$d0,$d1,$26,$e7,$e7,$26,$d0,$d1,$26,$26,$d0,$d1,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$db,$db,$26,$db,$db,$26,$db,$26,$26,$26,$db,$d5,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$db,$db,$26,$db,$db,$26,$e0,$dc,$26,$26,$db,$d1,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$d4,$d5,$26,$d4,$d5,$26,$d4,$d5,$26,$26,$db,$db,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$46,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$4a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$5f,$78,$78,$78,$78,$78,$78,$78,$78,$78,$78,$78,$78,$78,$78,$7a,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .org $2800
game:
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$28,$28,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24

  .org $E000
  palette:
  .db $22,$29,$1A,$0F,  $22,$36,$17,$0F,  $22,$30,$21,$0F,  $22,$27,$17,$0F   ;;background palette
  .db $22,$1C,$15,$14,  $22,$02,$38,$3C,  $22,$1C,$15,$14,  $22,$02,$38,$3C   ;;sprite palette

sprites:
     ;vert tile attr horiz
  .db $80, $32, $00, $80   ;sprite 0
  .db $80, $33, $00, $88   ;sprite 1
  .db $88, $34, $00, $80   ;sprite 2
  .db $88, $35, $00, $88   ;sprite 3

   .org $FFFA     ;first of the three vectors starts here
  .dw NMI        ;when an NMI happens (once per frame if enabled) the
                   ;processor will jump to the label NMI:
  .dw RESET      ;when the processor first turns on or is reset, it will jump
                   ;to the label RESET:
  .dw 0          ;external interrupt IRQ is not used in this tutorial

  .bank 2                       ; Change to bank 2
  .org $0000                    ; start at $0000
  .incbin "mario.chr"

I understand that you have to set high and low bits at $2000 but i still don't know how it functions either. I'm also trying to code multiple gamestates with different backgrounds. If anybody has a idea of what i'm doing wrong please let know.

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Thu Mar 28, 2019 9:36 pm 
Offline
User avatar

Joined: Thu Mar 31, 2016 11:15 am
Posts: 529
Didn't read all your code, but I noticed here you're only copying 128 bytes:

Code:
Background
   LDX #$00
   LoadBackgroundLoop:
   LDA title, x   ; load data from address (background + the value in x)
   STA $2007             ; write to PPU
   INX                   ; X = X + 1
   CPX #$80              ; Compare X to hex $80, decimal 128 - copying 128 bytes
   BNE LoadBackgroundLoop  ; Branch to LoadBackgroundLoop if compare was Not Equal to zero
                        ; if compare was equal to 128, keep going down
   RTS


A nametable is 1024 bytes. Typically you'd copy them all.


Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Fri Mar 29, 2019 4:18 am 
Online
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 2564
Location: DIGDUG
You will want to set up a pointer in the zero page (reserve 2 bytes) and use Indirect Indexed mode.

store the low byte of the address of data at pointer and high byte at pointer+1

LDA (pointer), y

INY each loop. BNE loop.
then outer loop, inc pointer+1, and INX till X=4 (CPX #4, BNE)

256*4 = 1024

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Fri Mar 29, 2019 9:52 am 
Offline

Joined: Mon Mar 25, 2019 10:58 am
Posts: 13
dougeff wrote:
You will want to set up a pointer in the zero page (reserve 2 bytes) and use Indirect Indexed mode.

store the low byte of the address of data at pointer and high byte at pointer+1

LDA (pointer), y

INY each loop. BNE loop.
then outer loop, inc pointer+1, and INX till X=4 (CPX #4, BNE)

256*4 = 1024


I coded with what you said and i also took from the N.N. comments and I got this:

Code:
LoadBackground:
  LDA $2002             ; read PPU status to reset the high/low latch
  LDA #$20
  STA $2006             ; write the high byte of $2000 address
  LDA #$00
  STA $2006             ; write the low byte of $2000 address
  LDY #$00
LoadBackgroundoutside:
  LDX #$00              ; start out at 0
LoadBackgroundLoop:
  LDA title, x     ; load data from address (background + the value in x)
  STA $2007             ; write to PPU
  INX                   ; X = X + 1
  CPX #$FF              ; Compare X to hex $80, decimal 128 - copying 128 bytes
  BNE LoadBackgroundLoop  ; Branch to LoadBackgroundLoop if compare was Not Equal to zero
                        ; if compare was equal to 128, keep going down
  INY
  CPY #$04
  BNE LoadBackgroundoutside
  RTS

But when I ran it... It resulted in a huge amount of graphical glitchs. I'm absolutely stumped at this point.....
EDIT: I added the actual rom so you can see what i'm talking about


Attachments:
main.nes [24.02 KiB]
Downloaded 158 times
Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Fri Mar 29, 2019 11:31 am 
Online
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 2564
Location: DIGDUG
Indirect Indexed mode.

LDA (pointer), y

in nesasm, use this syntax

LDA [pointer], y


See rainwarrior's reply here
viewtopic.php?f=10&t=17756

And the code here at loadnametables
https://forums.nesdev.com/viewtopic.php?f=10&t=4807

except change LDX #$01 to LDX #$04

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Fri Mar 29, 2019 6:49 pm 
Offline

Joined: Mon Mar 25, 2019 10:58 am
Posts: 13
Could I maybe utilize .nam name table files instead of using standard .db nametable setup like this?
Code:
.db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$28,$28,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$25,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
   


Is it easier to use .nam files or do you HAVE to use .db with some .nam integration?
EDIT: Yes! You can use .nam nametables. However 1/4 of the screen is displaying my data and the rest is showing sprite tile $00.


Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Sat Mar 30, 2019 6:11 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 11412
Location: Rio de Janeiro - Brazil
Well, you are copying only 256 bytes, which is equivalent to 8 name table rows (256 / 32 = 8). To copy the full 1024 bytes (960 NT bytes + 64 AT bytes) you need to use indexed indirect addressing, like advised above. That or use 4 loops like the one you're already using, each reading 1/4 of the data. Indexed indirect is much more versatile though, because a single loop can be used to read name tables from anywhere, you just need to change the pointer.


Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Sun Mar 31, 2019 10:15 am 
Offline

Joined: Mon Mar 25, 2019 10:58 am
Posts: 13
tokumaru wrote:
Well, you are copying only 256 bytes, which is equivalent to 8 name table rows (256 / 32 = 8). To copy the full 1024 bytes (960 NT bytes + 64 AT bytes) you need to use indexed indirect addressing, like advised above. That or use 4 loops like the one you're already using, each reading 1/4 of the data. Indexed indirect is much more versatile though, because a single loop can be used to read name tables from anywhere, you just need to change the pointer.

I'm deathly confused... Is it suppose to be indirect indexed of indexed indirect. This is what i coded.
Code:
LoadBackground:
      LDX #$00                ; set to beginning of first nametable
        STX $2006
        LDX #$ff
        STX $2006
      STY $2006
      LDX #$04
      LDA #low(Playscreen)
      STA $0000
      LDA #high(Playscreen)
      STA $0001

NameLoop:                       ; loop to draw entire nametable
        LDA ($2000),y
        STA $2007
        INY
        BNE NameLoop
        INC $16
        DEX

        BNE NameLoop
 
        RTS

I was told the "LDA ($2000),y" was indirect Indexed.

P.S. Is the rest of the code correct or am i just too stupid to figure out background code.


Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Sun Mar 31, 2019 10:53 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 11412
Location: Rio de Janeiro - Brazil
One thing you should really start doing is naming your variables. Using hardcoded hex values makes the code very hard to read and maintain.

Anyway, the main thing that's wrong here is that you're setting up a pointer at addresses $0000-$0001, but later you're doing LDA ($2000),y... your pointer is at address $0000 in ZP, so you should actually be doing LDA ($00), y (or LDA [$00], y if you're using NESASM, because it uses [] for indirection, unlike normal 6502 syntax), or you won't be using the pointer at all.

Your next mistake is INC $16. Again, what's at $16? Your pointer is at $0000-$0001! The high byte of the pointer is at $0001, so you should be doing INC $01. If you start naming your variables, it'll be much easier to reference them without making these kinds of mistakes.

The other thing that looks weird is when you set $2006... You're writing to it 3 times, and with weird values... You should just be writing $20, then $00, to have the PPU point to the first name table ($2000).


Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Sun Mar 31, 2019 2:02 pm 
Offline

Joined: Mon Mar 25, 2019 10:58 am
Posts: 13
Finally i fixed it. I tried dividing the background into 4 sections and writing the same background drawing code 3 more times but that still didnt work. Turns out i put the tile data in $2000 when im suppose to put it in $E000. and then it worked. Here what i did for the drawing code:
Code:
LoadBackground:
  LDA $2002         
  LDA #$20
  STA $2006         
  LDA #$00
  STA $2006           
  LDX #$00         
LoadBackground1:
  LDA background1, x     
  STA $2007         
  INX                 
  CPX #$00         
  BNE LoadBackground1 
         

LoadBackground2:
  LDA background2, x   
  STA $2007         
  INX                 
  CPX #$00             
  BNE LoadBackground2
                 

LoadBackground3:
  LDA background3, x   
  STA $2007           
  INX               
  CPX #$00         
  BNE LoadBackground3 
                   

LoadBackground4:
  LDA background4, x   
  STA $2007             
  INX                   
  CPX #$00     
  BNE LoadBackground4
 

And heres the nametable:
Code:
background1:
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$28
  .db $28,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 

  .db $24,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26
  .db $26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$26,$24

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24

background2:
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24

  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24
  .db $24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24,$24 

Is it practical for nesdevs to code it like this or do they have another method. To me this one works for games on a single screen\background.


Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Sun Mar 31, 2019 3:13 pm 
Online
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 2564
Location: DIGDUG
Quote:
Is it practical for nesdevs to code it like this or do they have another method


It is not practical, due to to limited ROM space. You could make a very small, non-scrolling game this way.

You will want to learn ways to compress data, whether a simple algorithm, like RLE, or a more complex method, like what Super Mario Bros does, like 2 byte represents "draw 5 bricks starting at x=5*16, y=7*16.

I use metatiles, where 1 byte represents a 16x16 square of the screen, and 1 color palette for that block.

With this you can compress a room from 1024 to 240 bytes.

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Top
 Profile  
 
 Post subject: Re: Cant Draw Background
PostPosted: Sun Mar 31, 2019 5:09 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 11412
Location: Rio de Janeiro - Brazil
Yes, I mentioned this method in a previous post. The main disadvantage is that you'll need 4 loops for each screen you need to display, and that's not very practical. With indirect indexed addressing you can read as many screens as you want from anywhere in the ROM using a single loop.

If you only have one or two screens you can go with the "4 loops" approach, since you're still a beginner, but you will eventually need to understand the other addressing modes if you want to make a complete game.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group