Author Topic: Assembly Help.  (Read 672 times)

Punch

  • Hero Member
  • *****
  • Posts: 3278
Assembly Help.
« on: May 02, 2013, 01:36:39 PM »
I'm trying to learn how to program for the PC Engine, I decided that I want to see the underlying code instead of using libraries + C translator... I don't have anything against HuC, actually it's great and easy, but since I've played around with NESASM a little bit I felt that I really wanted to learn to program in Assembler language.

But I need some help... I think I managed to figure out startup but I have some questions:

1- In the NES you had a specific memory space to upload characters (or it would be a ROM)... where is it on the PCE? I'm using $4000 VRAM address as in the MagicKit docs, but how does the PCE knows that the character data is there? Which leads to the second question, which I'm sure it is part of the first one.

2- The "BAT" doesn't store a simple index number like the NES, but the actual VRAM address + palette, right? Am I pointing to the first character (resident on $4000 onwards) by writing the value $0200 on VRAM address $0000? ($0200 * $20 = $4000).

3- Is there a debug friendly PCE emulator? I really loved FCEUXD SP and how you could see visually the nametables and characters uploaded into the PPU... is there something like it to see a visual representation of what data the PCE video processor chip is holding?

4- Now I'm going to be a moron and post my code... yes I know that asking to be spoonfed with solutions on a forum is bad but if I didn't get my issues covered with my first two questions you guys could help me... currently when I boot my compiled code into ootake it shows some dots plastered all over screen (as if I didn't upload the tiles correctly or as if I didn't write the character tile map in a recognisable format) when the intent was to show a character at 0,0 of the background.

Code: [Select]
;Sample Program

.include "instructions.asm"

.ZP ;Zero Page Data
Generic_Ptr .ds 2

.BSS ;RAM area.
Hi_Score .ds 2
Score_P1 .ds 2
Score_P2 .ds 2

.DATA
.bank 1
.org $4000
;Insert font here.
Ffont:
.incchr "Grafx/Stencil.PCX"
Fpal:
.incpal "Grafx/Stencil.PCX"

.CODE ;Start of ROM data.
.bank 0
.org $E000

BREAK:
RTI

VDC:
RTI

TIMER:
ACK ;STA $1403 macro.
RTI

NMI:
RTI

RESET:
LDA #$FF
TAM #0 ;Map I/O at bank 0
LDA #$F8
TAM #1 ;Map RAM at bank 1
LDA #$01
TAM #2 ;Map ROM bank 1 at bank 2

JSR LOADTXT; Loads Font into VRAM
; Also, this same subroutine writes a character at 0,0
; At least is supposed to.
.1
JMP .1


LOADTXT:
LDA #$09
STA $0000
LDA #%00000101
STA $0003

LDA #$00
STA $0000
LDA #$00
STA $0002
STA $0003

LDA #$02
STA $0000 ;Register #2 = VRAM read/write register
LDA #$40
STA $0002
LDA #$02
STA $0003 ;Write %0000 0010 0000 0000 to VRAM address $0000
;(Sets tile 0,0 to the character at $4000?)

LDA #$0F
STA $0000 ;Register #$0F = DMA Control Register
LDA #$00
STA $0002
STA $0003 ;No DSR DMA, Increment destination/source, no interrupts

LDA #$10 ;Register #$10 = DMA Source Address
STA $0000
LDA #LOW(Ffont); + 32
STA $0002
LDA #HIGH(Ffont); + 32
STA $0003 ;Set Source Address = Font address in ROM.

LDA #$11 ;Register #$11 = DMA Destination
STA $0000
LDA #$00
STA $0002
LDA #$40
STA $0003 ;Transfer font to VRAM $4000
;

LDA #$12 ;Register @#12 = DMA Length Register
STA $0000
LDA #$00
STA $0002
LDA #$08
STA $0003 ;Set block size to #$800 or 64 tiles
;Start transfer! (???)

LDX #$00
STX $0402
STX $0403 ;Choose color 0
LDA #$00
STA $0404
STA $0405 ;write some random color and change the BG to other than white

LDA #$05
STA $0000
LDA #%10100000
STA $0002  ;Turns on BG rendering...
RTS



 .org $FFF6

 .dw BREAK
 .dw VDC
 .dw TIMER
 .dw NMI
 .dw RESET

Punch

  • Hero Member
  • *****
  • Posts: 3278
Re: Assembly Help.
« Reply #1 on: May 02, 2013, 01:40:02 PM »
Oh and by the way, here is the "Stencil.PCX" file converted to PNG (yes I know about Photoshop palette inversion but I double checked with gimp and Grafx2)


Arkhan

  • Hero Member
  • *****
  • Posts: 14142
  • Fuck Elmer.
    • Incessant Negativity Software
Re: Assembly Help.
« Reply #2 on: May 02, 2013, 02:03:21 PM »
Use Mednafen for debugging PCE stuff.  Alt+D = debugger.  Alt+2 = VRAM viewer!


I suggest you read the docs that come with HuC/Magic Kit.  They explain much of what you are asking, and have things you can refer back to.


They also show you examples of what you are trying to do.

PCEAS (assembler) has macros that do loading as well.  Alot of the HuC stuff ends up just calling that crap.


You should also get savvy on 6502.  It's a kind of braindamaged assembly language (imho).  It's strange.
[Fri 19:34]<nectarsis> been wanting to try that one for awhile now Ope
[Fri 19:33]<Opethian> l;ol huge dong

I'm a max level Forum Warrior.  I'm immortal.
If you're not ready to defend your claims, don't post em.

ccovell

  • Hero Member
  • *****
  • Posts: 2245
Re: Assembly Help.
« Reply #3 on: May 02, 2013, 02:41:05 PM »
There is no specific VRAM address for tiles, sprites, etc.  It's all one 64K pool of VRAM.  The BAT (map) will start at VRAM $0000 and gets larger as you set different BAT dimensions.

Anyway, my advice is never to set anything at 0,0 because usually on consoles, that location is offscreen!  Same goes for Sprites.

Put something at $80,$80 and you can at least see WHAT you have put there.

Good luck programming!

TheOldMan

  • Hero Member
  • *****
  • Posts: 958
Re: Assembly Help.
« Reply #4 on: May 02, 2013, 03:11:49 PM »
Quote
how does the PCE knows that the character data is there?
It doesn't. Characters are done via background tiles that just happen to look like writing.

Quote
The "BAT" doesn't store a simple index number like the NES, but the actual VRAM address + palette, right?
Correct. High 4 bits are palette index, low 12 bits are address /16

Quote
Am I pointing to the first character (resident on $4000 onwards) by writing the value $0200 on VRAM address $0000? ($0200 * $20 = $4000).

No. If your 'font' is at $4000, you need to write  x400. That's the address in VRAM of the tile / 16.
Note that Vram adresses are in words.

Quote
(as if I didn't upload the tiles correctly or as if I didn't write the character tile map in a recognisable format)
Sorry, I don't even see where you loaded the tiles at all.
You did read the docs about DMA being Vram only, right?
I could be wrong about that, but I'm pretty sure that's why the original startup code uses a loop....

Punch

  • Hero Member
  • *****
  • Posts: 3278
Re: Assembly Help.
« Reply #5 on: May 03, 2013, 04:19:17 AM »
So the DMA is VRAM to VRAM only? And I specified the tiles incorrectly? #-o

Thanks for the help! I loaded the rom into Mednafen, Arkhan, and it doesn't show anything at all on the VRAM viewer... not surprising since I didn't upload character data at all to the 6270.

By the way, with "character" I meant a single tile, as in NES terminology... I'm a noob but not that much :P

Arkhan

  • Hero Member
  • *****
  • Posts: 14142
  • Fuck Elmer.
    • Incessant Negativity Software
Re: Assembly Help.
« Reply #6 on: May 03, 2013, 04:42:23 AM »
So the DMA is VRAM to VRAM only? And I specified the tiles incorrectly? #-o

Thanks for the help! I loaded the rom into Mednafen, Arkhan, and it doesn't show anything at all on the VRAM viewer... not surprising since I didn't upload character data at all to the 6270.

By the way, with "character" I meant a single tile, as in NES terminology... I'm a noob but not that much :P

If you don't see anything, then you have something wrong.

However, if you don't do ANYTHING with your code, the default startup code should put the default font set into VRAM.  Try that and see if it works.

There is a little doc that comes with MagicKit that will show you all of this stuff though.  It's probably easier to go check that out.   I think it's called VDC.txt.

[Fri 19:34]<nectarsis> been wanting to try that one for awhile now Ope
[Fri 19:33]<Opethian> l;ol huge dong

I'm a max level Forum Warrior.  I'm immortal.
If you're not ready to defend your claims, don't post em.

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: Assembly Help.
« Reply #7 on: May 05, 2013, 06:23:41 PM »
This might help some:

Code: [Select]
;Sample Program

  ;.include "instructions.asm"

  .ZP   ;Zero Page Data
Generic_Ptr .ds 2

  .BSS  ;RAM area.
Hi_Score  .ds 2
Score_P1  .ds 2
Score_P2  .ds 2

  .DATA
  .bank 1
  .org $4000
  ;Insert font here.
Ffont: .incchr "Grafx/Stencil.PCX"
Fpal: .incpal "Grafx/Stencil.PCX"

  .CODE ;Start of ROM data.
  .bank 0
  .org $E000

BREAK:
  RTI
  
VDC:
  pha
  lda $0000 ; acknowledge VDC interrupt
  
  ;your v-int and h-int code here
  
  pla

  RTI
  
TIMER:
  stz $1403 ;STA $1403 macro.
  RTI
  
NMI:
  RTI
  
RESET:
  SEI
  NOP
  CLD
  LDA #$FF
  TAM #0  ;Map I/O at bank 0
  LDA #$F8
  TAM #1  ;Map RAM at bank 1
  LDA #$01
  TAM #2  ;Map ROM bank 1 at bank 2
  
          ;turn off display
  st0 #$05
  st1 #$00
  st2 #$00

          
          ;initialize ram to #$00
  stz $2000
  tii $2000,$2001,$1fff
  
  lda #%00000101 ;turn on VDC interrupt, turn off the rest.
  sta $1402
  
          ;initialize TIMER
  stz $c00
  stz $c01
  
          ; set master volume to zero/off
  stz $801
  
  
  JSR LOADTXT; Loads Font into VRAM
  ;     Also, this same subroutine writes a character at 0,0
  ;     At least is supposed to.\
  
  jsr init_vdc
  
    cli ; Set VDC interrupt in action
.1
  JMP .1
  
  
LOADTXT:

            ;set vram address to $4000
  st0 #$00
  st1 #$00
  st2 #$40
  
            ;set vram to read/write mode
  st0 #$02

            ;map character data to cpu/logical memory map
  lda #bank(Ffont)
  tam #$02
  inc a
  tam #$03
  
            ;dma data from cpu to vdc
  tia (Ffont & $1fff)+$4000,$0002,sizeof(Ffont)

  lda #bank(Fpal)
  tam #$02
  inc a
  tam #$03

  stz $402
  stz $403
            ;dma dma from cpu to vce
            ;Note: there's a bug in the PCX import for pal data, hence the $1e0 offset for 16 color image
  tia (Fpal & $1fff)+$4000+$1e0,$404,$20

;clear tilemap
  st0 #$00
  st1 #$00
  st2 #$00
  st0 #$02
  clx
  ldy #$10
.loop1
  stz $0002
  stz $0003
  dex
  bne .loop1
  dey
  bne .loop1
  
;show character set example
  st0 #$00
  st1 #$00
  st2 #$00
  st0 #$02
  clx
  ldy #$10
.loop2
  sta $0002
  inc a
  and #$3f
  st2 #$04
  dex
  bne .loop2
  dey
  bne .loop2
  
;turn on VDC
  st0 #$05
  st1 #$8c ;turn on BG, no sprites, h-int, and v-int
  

  RTS
  
  
init_vdc:
  ; init VDC
  
    st0 #MAWR
    st1 #$00
    st2 #$00    
    st0 #MARR
    st1 #$00
    st2 #$00    
    st0 #BXR
    st1 #$00
    st2 #$00
    st0 #BYR
    st1 #$00
    st2 #$00
    st0 #MWR ;64x64 BAT
    st1 #$50
    st2 #$00
    st0 #VSR  ;0F02
    st1 #$02
    st2 #$17
    st0 #VDR  ;$00E0  224 vertical res
    st1 #$db
    st2 #$00
    st0 #VDE  ;$0003  
    st1 #$1b
    st2 #$00
    st0 #DCR
    st1 #$00
    st2 #$00
    st0 #HSR  ;$0202
    st1 #$02
    st2 #$02
    st0 #HDR  ;$031F  256 horizontal res
    st1 #$1f
    st2 #$03
    st0 #SATB
    st1 #$00
    st2 #$7f
  rts

; VDC equates

VDC_STAT .equ $00   ;VDC status reg
MAWR  .equ  $00   ;Memory Access Write Reg
MARR  .equ  $01   ;Memory Access Read Reg
VRWR  .equ  $02   ;Vram Read/Write reg
CR  .equ  $05   ;Control Reg
RCR .equ  $06   ;Raster Control Reg
BXR .equ  $07   ;Background X(scroll) Reg
BYR .equ  $08   ;Background Y(scroll) Reg
MWR .equ  $09   ;Memory Access Width Reg
HSR .equ  $0a   ;Horizontal Synchro Reg
HDR .equ  $0b   ;Horizontal Display Reg
VSR .equ  $0c   ;Vertical Synchro Reg
VDR .equ  $0d   ;Vertical Display Reg
VDE .equ  $0e   ;Vertical Display End Reg
DCR .equ  $0f   ;DMA Control Reg
DSR .equ  $10   ;DMA Source Address Reg
DDR .equ  $11   ;DMA Destination Address Reg
DBR .equ  $12   ;DMA Block Length Reg
SATB  .equ  $13   ;VRAM-SATB Source Address Reg
L_RES .equ  $04   ;(5.37mhz) 256
M_RES .equ  $05   ;(7.16mhz) 352
H_RES .equ  $06   ;(10.5mhz) 512

 .org $FFF6

 .dw BREAK
 .dw VDC
 .dw TIMER
 .dw NMI
 .dw RESET

 Here's a some links:
http://pcedev.wordpress.com/downloads-and-links/
http://www.pcedev.net/blog/files/Otaku_no_PCE_cribsheet_page1_v1_0_3.png
http://www.pcedev.net/blog/files/Otaku_no_PCE_cribsheet_page2_0_1_4.png

(I'm tired and it's late, so forgive any mistakes. It assembles and runs for me)
« Last Edit: May 05, 2013, 06:26:58 PM by Bonknuts »

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: Assembly Help.
« Reply #8 on: May 06, 2013, 04:03:10 AM »
Also, this might help explain some VDC details: http://pastebin.com/QeXxMcja


Arkhan

  • Hero Member
  • *****
  • Posts: 14142
  • Fuck Elmer.
    • Incessant Negativity Software
Re: Assembly Help.
« Reply #9 on: May 06, 2013, 04:31:55 AM »
Isn't that the doc that comes with PCEAS/HuC?

I forget.    There's a similar one at least. 
[Fri 19:34]<nectarsis> been wanting to try that one for awhile now Ope
[Fri 19:33]<Opethian> l;ol huge dong

I'm a max level Forum Warrior.  I'm immortal.
If you're not ready to defend your claims, don't post em.

ccovell

  • Hero Member
  • *****
  • Posts: 2245
Re: Assembly Help.
« Reply #10 on: May 06, 2013, 12:29:11 PM »
Charles MacDonald's PCE tech document has proven very valuable to me:   http://www.romhacking.net/documents/302/

Arkhan

  • Hero Member
  • *****
  • Posts: 14142
  • Fuck Elmer.
    • Incessant Negativity Software
Re: Assembly Help.
« Reply #11 on: May 07, 2013, 03:59:08 AM »
yeah pretty much anything ChrlyMac writes is legit. 


[Fri 19:34]<nectarsis> been wanting to try that one for awhile now Ope
[Fri 19:33]<Opethian> l;ol huge dong

I'm a max level Forum Warrior.  I'm immortal.
If you're not ready to defend your claims, don't post em.

Bonknuts

  • Hero Member
  • *****
  • Posts: 3292
Re: Assembly Help.
« Reply #12 on: May 07, 2013, 05:18:00 AM »
Isn't that the doc that comes with PCEAS/HuC?

I forget.    There's a similar one at least.  


 No, it's the fourth chapter on a tutorial for coding assembly on the PCE, that I did a number of yeas back.


Quote
Charles MacDonald's PCE tech document has proven very valuable to me:   http://www.romhacking.net/documents/302/


 Yeah, that doc was invaluable when I was first starting out. There wasn't much docs and lot of them had errors. Charlie Mac's stuff is legit. Though I never reposted or hosted that doc because his disclaimer in it:

Quote
Regarding distribution, you cannot put this document on another
 website, nor link directly to it.


 I guess RHDN doesn't give a shit.



 Another file that might help when you get to doing sprites: http://www.pcedev.net/pce_gfx/sprite_chart/sprite-size-diagram1.png
It has to do with the alignment of the sprite cells in vram, when they are larger than 16x16.
« Last Edit: May 07, 2013, 05:21:17 AM by Bonknuts »

Arkhan

  • Hero Member
  • *****
  • Posts: 14142
  • Fuck Elmer.
    • Incessant Negativity Software
Re: Assembly Help.
« Reply #13 on: May 07, 2013, 06:18:03 AM »
No, it's the fourth chapter on a tutorial for coding assembly on the PCE, that I did a number of yeas back.

Ah, ok.

I don't think I ever actually saw your tutorial.  Is the whole thing floating around somewhere? 

Also, did the OP vanish?  Right now it appears to just be a circle jerk of us suggesting things, lol
[Fri 19:34]<nectarsis> been wanting to try that one for awhile now Ope
[Fri 19:33]<Opethian> l;ol huge dong

I'm a max level Forum Warrior.  I'm immortal.
If you're not ready to defend your claims, don't post em.

Punch

  • Hero Member
  • *****
  • Posts: 3278
Re: Assembly Help.
« Reply #14 on: May 07, 2013, 11:04:40 AM »
Sorry, I just moved to another city, and I had some problems to solve. But I promise I'll analyse your guys' postings, thanks a lot!