home *** CD-ROM | disk | FTP | other *** search
File List | 2007-01-01 | 6.1 KB | 217 lines |
- uTHE HEX FILES PART 3 written by Jason
-
- Welcome again, here's the third
- instalment of Hex Files for your
- delight, delectation & several other
- words beginning with the letter D that
- sound quite good. And, before we start
- proper, I'll just give you the solution
- to the little teaser I posed at the end
- of the previous article.
-
- Now, one of the joys of machine code is
- that there are several ways to reach
- the same solution and, whilst some are
- better than others, they're all valid
- at this stage in the game.
-
- So, whilst there are other methods that
- could be used (if you found & used it,
- it's not the wrong answer!) the easiest
- way to modify the routine in the last
- instalment would be something like:
-
- * = $0900
- ldx #$00
- lda #$03 ; change the character loop
- sta $0400,x
- inx
- cpx #$0b ; change the number of repeats
- bne loop
- rts
-
- Okay, lets have some fun with a loop.
- One of the most common things in demos
- (& in fact most games too) is the
- scrolling message & what we are going
- to do is a simple one with some limits.
- As we have seen from the example last
- issue it's possible to put characters
- on the screen very fast, so fast in
- fact that we can't actually see it
- happen.
-
- It's also possible to move characters
- around the screen using loops. Start up
- your text editor, tab a couple of times
- to get the cursor to its start position
- & enter this program:
-
- * = $0900
- main ldx #$00
- move_loop lda $0401,x
- sta $0400,x
- inx
- cpx #$27
- bne move_loop
- inc $0427
- jmp main
-
- Before we run it lets look at what you
- have typed. The * command is as we used
- before, telling C64Asm we want our code
- at $0900 (again, 2304 in decimal). We
- then have a label called main which is
- the start of our main code (hence the
- name!) & that clears the X register
- again as we have done before. The main
- loop of the program (named with the
- appropriate label again) is new though.
-
- It reads from screen position $0401 &
- puts whatever it has read into $0400.
- Then the X register goes up one & it
- repeats that until X reaches $27 (which
- is 39 in decimal). Why stop at 39?
- Well, by the time X gets to 40 (by the
- time we're checking X it's been
- INCremented, remember) the routine is
- reading from $0428 (the start of the
- second line of the screen) & writing to
- $0427 (the right hand end of the first
- line) so if we wait we would be reading
- the first character of the next line of
- the screen! Finally we just play with
- the character at the top right of the
- screen to make something to look at (by
- constantly INCrementing it to make it
- show every character the C64 has).
-
- Okay, lets crank it up & watch it go!
- Assembling is as before, SAVE the file
- from the text editor as scroll.asm (the
- extension meaning "assembly code", not
- even vaguely essential to the process
- but it makes remembering what the files
- are a lot easier), type c64asm
- scroll.asm scroll.prg from DOS whilst
- in the correct directory & finally drag
- & drop the PRG into WinVICE & SYS2304
- to start it.
-
- Oh! Now something is happening but
- because machine code is so fast we
- can't see what, so we need to slow
- things down a bit & to do that I'll
- introduce a new friend in the form of a
- location in the VIC-II chip.
-
- Location $D012 (or 53,266 in decimal)
- is known as the raster register. The
- raster is a line that moves down the
- C64's screen redrawing it fifty times a
- second & there are over three hundred
- "raster lines" on a standard PAL C64
- (there are less lines & a faster
- refresh speed for NTSC machines) & it's
- possible to wait for a specific line &
- do something when you get there. Lets
- alter our example to take advantage of
- this, go back to the source & enter the
- following just after the ldx #$00 on
- the second line:
-
- lda #$fe
- raster cmp $d012
- bne raster
-
- SAVE it back out this time as
- scroll2.asm & assemble it as before.
- These three new lines set the A
- register up with a value of $fe (254 in
- decimal), then compare that to whatever
- $D012 contains in the same way as we
- compared numbers in the previous
- installment & if it's not the same (in
- other words if the raster isn't at
- position $fe) then the Branch if Not
- Equal (BNE) back to raster keeps it
- waiting in that loop until it is.
-
- You should now see loads of characters
- scrolling across the top of the screen
- very fast but not so fast that you
- can't see what's going on. Don't worry
- about the odd jump, we are only
- experimenting at this point & every now
- & then the C64 will miss a beat because
- it's busy doing it's housekeeping at
- rasterline $fe. Believe it or not this
- is moving fifty times a second! Okay,
- so one final trick for our new listing
- I think, flip back over to the text
- editor & alter the routine to read like
- this:
-
- * = $0900
- ldy #$00 ; this is new
- main ldx #$00
- lda #$fe
- raster cmp $d012
- bne raster
- move_loop lda $0401,x
- sta $0400,x
- inx
- cpx #$27
- bne move_loop
- lda $a1ff,y ; this line is new too
- sta $0427 ; the INC command used to
- be here
- iny ; & this line is new as well
- jmp main
-
- SAVE the source out as scroll3.asm,
- assemble & execute again, & if you
- press the SHIFT & Commodore keys you'll
- see words flying across your screen!
- What we are actually doing in these new
- bits is using the Y register as a
- counter & reading from the C64's memory
- at $A1FF onwards for 256 bytes. So
- where do the words come from? Well,
- $A1FF is actually where the C64 keeps
- some of its error messages & this is
- what you're seeing.
-
- Well, I think that's about enough for
- this installment but before I go
- another couple of little challenges for
- you all to see if you've got the gist;
- at the moment the routine reads it's
- data from location $A1FF but can you
- change it to read from $E460? And can
- you make it work on the second line of
- the screen rather than the first
- (remember to change all of the
- references to the screen). I'll give
- the answers in the next thrilling
- installment but If you have any
- questions about this article, machine
- code or Flamenco dancing, email me &
- I'll see what I can do. Oh, except
- about the dancing.
-
- The source code for the routines above
- can be downloaded:
-
- http://www.oldschool-gaming.com/
- files/c64/hex_files/
- part_3_files.zipfiles/c64/hex_files/
- part_3_files.zip
- for easier reference.
-
- Printed with Permission from Jason
- Taken from the Oldschool gaming website
- http://www.oldschool-gaming.com/
- c64_hex_files.php
-