home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / genie-commodore-file-library / Information / BONKRS03.TXT < prev    next >
Encoding:
Text File  |  2019-04-13  |  21.4 KB  |  574 lines

  1.                             _Bonkers_
  2.  
  3. Level #03
  4.                         (c) Curtis White 
  5.  
  6.          FREEWARE: NO SALE OF THIS MAGAZINE IS ALLOWED WITHOUT
  7.                     PRIOR WRITTEN PERMISSION.
  8.  
  9. This time we have a ton of stuff that we will be covering! We will make
  10. the leap from the monitor to the assembler, start building our library
  11. of routines, and maybe learn a few more instructions. No time to waste;
  12. let's venture on!
  13.                                                      The Bonker's Staff
  14.  
  15. Editor: Coolhand             < Send your opinions, thanks,
  16. Technical Dude: Dokken       < and gripes to these clowns!
  17. Author: Light                < 
  18. Support Web Site:        
  19. http://soho.ios.com/~coolhnd/bonkers/planet.htm
  20.  
  21.  
  22. Stage #01 Info Highway equals one way street and 23,000 cars going in
  23. both directions.
  24.  
  25. First, we have been coding in the machine language monitor which, as we
  26. know, is used primarily for viewing and changing memory locations. Now,
  27. we will be coding in an assembler, which will allow us to edit and
  28. understand our code better by using labels, directives, comments, and
  29. symbols.
  30.  
  31. There are several good assemblers for the c64/128.  It is not the
  32. purpose of this magazine to determine which is the best for you. But all
  33. of our source code will be Turbo Assembler compatible; we will list some
  34. common Turbo Assembler commands below, followed by common assembly
  35. terms.
  36.  
  37. +----------------------------------------------------------------------+
  38. |                       Turbo Assembler Commands                       |
  39. +----------------------------------------------------------------------+
  40.  
  41. LEFT ARROW 2 - Comment line. 
  42. LEFT ARROW 3 - Assemble to memory.                        
  43. LEFT ARROW 5 - Assemble to disk.                        
  44. LEFT ARROW 1 - Return to basic.                         
  45. LEFT ARROW S - Save source to disk in program format.         
  46. LEFT ARROW W - Save source to disk in sequential format.      
  47. LEFT ARROW L - Load source from disk in program format.       
  48. LEFT ARROW E - Load source from disk in sequential format.    
  49. LEFT ARROW INST DEL - Delete line of code
  50.  
  51. +-----------------------------------------------------------------------+
  52. |                           Common Assembly Terms                       |
  53. +-----------------------------------------------------------------------+
  54.  
  55.   Assembler: Program that takes source code and produces object code.
  56.              Note each instruction in the source code becomes the same
  57.              object code instruction after it's assembled.
  58.  
  59.   Constants: As we have already learned, the prefix that determines the
  60.              number base used, like $ for hex and % for binary.
  61.  
  62.  Directives: These are used to communicate with the assembler. They are
  63.              powerful and they are used frequently.
  64.  
  65. Expressions: A string of operators (+, /, -, *), NOTE that these are
  66.              performed during assembly and not during run time. 
  67.       
  68.       Label: Name for a line of code in the assembler. This is used
  69.              to replace actual addresses that are used in the monitor.
  70.  
  71. Object Code: The code that the assembler makes and the computer runs.
  72.              Also the code that can be viewed in a monitor.
  73.  
  74.      Opcode: Machine/Assembly language instruction. Example  
  75.              LDA, STA, BEQ, RTS.
  76.  
  77.     Operand: Data which the opcode or instruction uses, not all 
  78.              instructions have operands.
  79.       
  80. Source Code: The code that you edit in the assembler, includes comments.
  81.  
  82.  
  83. Let's get started on our first assembly language program. We will cover
  84. every detail of this short program.
  85.  
  86.       *= 8192      ; Start address of object code
  87.  
  88. chrout = $ffd2
  89.  
  90. printa  lda #"a"   ; get letter a
  91.         jsr chrout ; print letter to screen
  92.         rts        ; return to basic
  93.  
  94. Note: to run this program either type 'SYS 8192' from BASIC or hit
  95. LEFT ARROW 3 from Turbo Assembler.
  96.  
  97. First the * is a directive which is used to change the program counter.
  98. The PC (Program Counter) as we know dictates what code is currently
  99. running and going to be executed or run. SYS 49152 sets the program
  100. counter to address 49152. The *= just assigns where our object code will
  101. be placed in memory and usually it is also the start address of our
  102. code.
  103.  
  104. The = is also a directive which has a variety of uses. In the example
  105. chrout = $ffd2, we assign the address $ffd2 to the label chrout.
  106.  
  107. Printa is a label which can used just as an address or location is used
  108. in the monitor.
  109.  
  110. The ; declares that comments are to follow and they are to be ignored
  111. when assembling, hence we can now type the comments in along with the
  112. actual code. The ; (semi-colon) is almost identical to the Basic command
  113. REM.
  114.  
  115. A Task... (Yeah right, who is going to ever use this?)
  116.  
  117. The task is to create a simple word processor which will allow us to
  118. leave memos on the computer and then highlight those memos. To use the
  119. program simply SYS 8192 or LEFT ARROW 3 (FOR TASM ONLY) and type your
  120. message (no cursor will be displayed). This sounds like fun so let's do
  121. it!
  122.  
  123.           *= 8192            ; start address
  124. chrout     = $ffd2           ; KERNAL ROUTINE TO OUTPUT A CHAR
  125. getin      = $ffe4           ; KERNAL ROUTINE TO INPUT A CHAR
  126.  
  127. ;//////////////////Setup Screen
  128.  
  129.              lda #$93        ;Clear screen char
  130.              jsr chrout      ;Clear it 
  131.              lda #$00        ;Color Black
  132.              sta $d020       ;53280 DECIMAL BORDER COLOR
  133.              sta $d021       ;53281 DECIMAL BACKGROUND COLOR
  134.  
  135. ;//////////////////Main Loop
  136.  
  137. main         jsr wordproc    ;INPUT/OUTPUT 
  138.              jsr memodisplay ;HIGHLIGHT BORDER
  139.              jmp main        ;BACK TO MAIN 
  140.  
  141. ;//////////////////Word Processor
  142.  
  143. wordproc     jsr getin       ;KERNAL ROUTINE TO GET CHAR  
  144.              jsr chrout      ;Now output char that getin copied
  145.              rts             ;Return to main loop
  146.  
  147. ;//////////////////Highlight Memo
  148.  
  149. memodisplay
  150.              ldx #$00        ;Set Index Register X To Value 0
  151. coloop       lda colors,x    ;Get data colors, indexed to X.
  152.              sta $d020       ;Store those colors 2 border
  153.              inx             ;Increase our index
  154.              cpx #9          ;Compare X TO 9 (9 colors)  
  155.              bne coloop      ;Branch coloop until 9 cols
  156.              rts             ;End Subroutine
  157.  
  158. colors       .byte 6,14,3,7,1,7,3,14,6
  159.                     
  160. Now, it is time to dissect the more interesting parts of the above code.
  161. First, notice the $d020/$d021 (53280/53281). These are memory locations
  162. that will change the entire border or background color of the screen.
  163. Sixteen unique colors are available for us to use (numbers 0 through
  164. 15), and it just so happens that zero is the color black.
  165.  
  166. Also, notice the JSR GETIN. We use this kernal subroutine to get a
  167. character from the keyboard into the A Register. Since the kernal
  168. subroutine CHROUT outputs the character that is stored in the A register
  169. to the screen, we can use these together to copy letters from the
  170. keyboard to the screen.
  171.  
  172. Notice the JMP (JUMP Instruction). This instruction is similar to the
  173. JSR (Jump Saving Return Address), except that the JMP instruction does
  174. not save the return address. For those who already know BASIC, you can
  175. compare it to the GOTO rather then the GOSUB, as with JSR.
  176.  
  177. Now, as a quick reminder that we are using an assembler, below is what
  178. the same code looks like in the MONITOR.
  179.  
  180. <<----------------------- BEGIN MACHINE LANGUAGE CODE
  181. . 8192 LDA #147
  182. . 8194 jsr 65490
  183. . 8197 lda #0
  184. . 8199 sta 53280
  185. . 8202 sta 53281
  186. . 8205 jsr 8214
  187. . 8208 jsr 8221
  188. . 8211 jmp 8205
  189. . 8214 jsr 65508
  190. . 8217 jsr 65490
  191. . 8220 rts
  192. . 8221 ldx #0
  193. . 8223 lda 8235,x
  194. . 8226 sta 53280
  195. . 8229 inx
  196. . 8230 cpx #9
  197. . 8232 bne -11 (223)
  198. . 8234 rts
  199. . 8235 b 6 14 3 7 1
  200. . 8240 b 7 3 14 6 3 
  201. <<---------------------- END MACHINE LANGUAGE CODE
  202.  
  203. Preparing to code a beast.. 
  204.  
  205. First, as with programming any computer, the systems architecture will
  206. be of utmost importance when deciding how to achieve the results that we
  207. want. We will not ignore the unique properties of the c64/128; but our
  208. goal is to explain machine language to the beginner.
  209.  
  210. Inside your c64/128 is a chip called the VIC II. It is responsible for
  211. everything you see on the screen. When we changed the background ($d021)
  212. and border ($d020) colors, we were changing the registers of the VIC II
  213. chip. It has several modes of operation, including high resolution,
  214. medium resolution, character set graphics, extended background color
  215. mode, multicolor mode, and many more features. We will cover those areas
  216. that are important to us, but we will not indulge on these topics. It is
  217. in your interest to learn more about these graphics features, as they
  218. can make some cool effects possible
  219.  
  220. Anyhow.. there is a mode called EXTENDED BACKGROUND COLOR mode. We can
  221. enable that mode with a basic statement of POKE53265,PEEK(53265)OR64.
  222. Now, if you have it enabled, type the following: lower case a, shifted
  223. a, reversed lower case a, shifted reverse a. As you can see, instead of
  224. getting the normal characters, you can have several background colors.
  225. In fact, we are limited to 64 characters, but we have a total of FOUR
  226. background colors. These are determined by the registers $d021 (NORMAL),
  227. $d022(SHIFTED), $d023 (REVERSED), and $d024 (REVERSE SHIFTED). So normal
  228. characters get their background color from $d021 as usual, shifted
  229. characters get their background color from $d022, and so on.
  230.  
  231. What if we could get reversed characters with these additional
  232. background colors? Then, cycle the colors in that background color
  233. register, and it would look as if the letters were changing colors. If
  234. you type some reversed text and change the background colors, you see
  235. that the background color shows through the letters. If you fill the
  236. screen with the character color and then cycle the background color, it
  237. looks as if the color of the letters is changing. But, in the normal
  238. text mode we only have two colors (foreground and background), so all
  239. the words would be cycling colors. This is no good if we want to draw
  240. attention to one word or a few words.
  241.  
  242. Now, with extended background color we can cycle three of the background
  243. registers with different sets of colors and leave one the same. But, it
  244. is not that simple, since we are limited to the first 64 characters.
  245. These are the normal characters and are not reversed. The character
  246. color would not change, and it would look as if the background colors
  247. were changing. This could be used to draw attention to words or letters.
  248. But I'd rather have the colors cycling inside the letters, wouldn't you?
  249.  
  250. So are we at a dead end? No! We can use what is called a custom
  251. character set. The characters are stored in ROM. The first 64 characters
  252. just happen to be the normal characters (not reversed like we need). The
  253. ROM character set is located at 53248 to 56832. Notice that these
  254. locations are the same as the VIC II chip's registers. The VIC II can
  255. swap memory in and out without much trouble; how the heck we don't care!
  256. But, if we swap out the VIC II and copy those locations to RAM and tell
  257. the VIC II to look in ram and not ROM for the characters, we can get our
  258. custom character set which equals reversed characters with the extended
  259. background color mode!
  260.  
  261. So, being the all powerful coder that I am. I know that 54272 through
  262. 54272+512 contain the sixty four reversed characters that we need. You
  263. can arrive at the 512 by multiplying 8 bytes of memory for each
  264. character times 64 characters. Really though, I never bother typing in
  265. numbers or code or anything - the tech dude does all the work. Below is
  266. what this looked like before the tech dude changed it.
  267.  
  268. (DIAG A1)
  269. So *insert arrogant words here*. I know that *tech word* through *tech
  270. word* will *explanation*. *Humble reader* *tech word*. Really though, I
  271. never bother *action* in numbers or *whatever* the tech dude does all
  272. the work. *REF DIAG A1* is what this looked like *REF DIAG A1* the tech
  273. dude *action*.
  274.  
  275. That is the way the entire magazine is drafted, no joke! 
  276.  
  277. -- Note from technical dude:  Not! --
  278.  
  279. Okay.. anyhow back to our awesome program. We need to set extended
  280. background color mode, copy the reversed characters to ram, and then
  281. tell the VIC II to look in RAM not ROM for the characters. Sounds like a
  282. mission briefing.. but maybe it will not be that hard. HAHAH
  283.  
  284. A Task..(The Best Note Maker Ever Published) -catchy!
  285.  
  286. Here is this awesome note maker.. with explanations throughout. This is
  287. not the final program, but we will make a full fledged operational note
  288. maker, perhaps even in this Level. To change colors you can SHIFT what
  289. you type, REVERSE on, and even SHIFT reverse. (Below is the documented
  290. program. I will repeat it without added comments)
  291.  
  292. ; Bonkers Note Maker v1
  293.  
  294.           *= 8192
  295. chrout     = $ffd2
  296. getin      = $ffe4
  297.  
  298.              lda #0
  299.              sta 646
  300.  
  301. Location 646 sets the foreground color which will actually be the
  302. background color sense our characters will be reversed!  On newer VIC
  303. chips, when #$96 is JSR'd to chrout, the value that is in 646 is used
  304. to initialize color memory.
  305.  
  306.              lda #$93
  307.              jsr chrout
  308.  
  309. Here we clear the screen.
  310.              
  311.              lda #$00
  312.              sta $d020
  313.  
  314. We set the border color register to black
  315.               
  316.              lda #$01
  317.              sta $d021
  318.  
  319. Do not be fooled.. ehhe, we are setting the background color register to
  320. white, but our characters are reversed so this is actually the
  321. foreground color.
  322.  
  323.           sei
  324.  
  325. This turns off the interrupts, it is a new opcode which you may use
  326. quite a bit or you may not use much at all. SEI (Set Enable Interrupt),
  327. turn em off!  We do this because we are switching out ROM. If an
  328. interrupt occured when ROM is switched out, the KERNAL calls during this
  329. time would crash our program!  This, by the way, would be bad.
  330.  
  331. copychar     lda 1
  332.          and #251
  333.              sta 1
  334.  
  335. What we do here is switch out the VIC chip's memory in our to access the
  336. ROM behind the chip. The AND is also a new opcode, it can be used to
  337. turn bits off. Do not worry about it, we will devote a lot of time to it
  338. in the future.
  339.  
  340.          ldx #$00
  341. Getchl       lda 54272,x
  342.              sta 12288,x
  343.              lda 54272+256,x
  344.              sta 12288+256,x
  345.              inx
  346.              cpx #255
  347.              bne getchl
  348.  
  349. Here is a loop to get our 64 characters. Notice that we use two sets of
  350. LDA/STA to access more then 256 locations, since the X register can hold
  351. only values 0 through 255.
  352.  
  353.              lda 1
  354.              ora #4
  355.              sta 1
  356.  
  357. Here we switch the VIC II back into memory. Notice that the ORA is a new
  358. opcode, it can be used to turn bits on. Again, we will devote several
  359. pages to these new bit operations.
  360.  
  361.              cli
  362.  
  363. Now that we have switched ROM back in, we allow the VIC II chip to
  364. interrupt so it can do its own maintenence things like processing
  365. keyboard inputs.
  366.  
  367.              lda 53272
  368.              and #240
  369.              ora #12
  370.              sta 53272
  371.  
  372. These 4 instructions, which do the same as the BASIC statement:
  373. poke 53272, (peek(53272) and 240) or 12, tells the VIC II chip to use
  374. the charset located at 12288 in RAM.  From now on, we use the
  375. reversed charset that we copied from ROM.
  376.  
  377.              lda 53265
  378.              ora #64
  379.              sta 53265
  380.  
  381. Here we set the VIC II chip to EXTENDED BACKGROUND COLOR mode, giving us
  382. access to four background colors.
  383.  
  384. main         jsr wordproc
  385.              jsr memodisplay
  386.              jmp main
  387.  
  388. The powerhouse is this loop. The processor will jsr wordproc, and when it
  389. gets to the rts, it will return to jsr memodisplay. The next instruction
  390. will be jmp main, and it will start all over again.
  391.  
  392. wordproc     jsr getin
  393.              jsr chrout
  394.              rts
  395.  
  396. This subroutine calls two kernal subroutines which get a keyboard
  397. character and output a keyboard character.
  398.  
  399. memodisplay  ldx #$00
  400. memloop      lda col1,x
  401.              sta 53282
  402.              lda col2,x
  403.              sta 53283
  404.              lda col3,x
  405.              sta 53284
  406.              inx
  407.              cpx #9
  408.              bne memloop
  409.              rts
  410.  
  411. This code puts 9 colors in each of the 3 registers for a smooth cycling
  412. effect.
  413.  
  414. col1      .byte 6,14,3,7,1,7,3,14,6
  415. col2      .byte 11,12,15,7,1,7,15,12,11
  416. col3      .byte 9,4,8,7,1,7,4,8,9
  417.  
  418. Here is our color data. First we have a smooth blue blend, then a gray
  419. blend, and finally an orange blend.
  420.  
  421. Here is the code without the comments. Note you can add your own
  422. comments with the ";" and make notes and reminders to yourself. It is
  423. always a good idea to comment programs.
  424.  
  425. ; Bonkers Note Maker v1
  426.  
  427.        *= 8192
  428. chrout  = $ffd2
  429. getin   = $ffe4
  430.           
  431.           lda #0
  432.           sta 646
  433.           lda #$93
  434.           jsr chrout
  435.           lda #$00
  436.           sta $d020
  437.           lda #$01
  438.           sta $d021
  439.           sei
  440. copychar  lda 1
  441.           and #251
  442.           sta 1
  443.           ldx #$00
  444. getchl    lda 54722,x
  445.           sta 12288,x
  446.           lda 54272+256,x
  447.           sta 12288+256,x
  448.           inx
  449.           cpx #255
  450.           bne getchl
  451.           lda 1
  452.           ora #4
  453.           sta 1
  454.           cli
  455.           lda 53272
  456.           and #240
  457.           ora #12
  458.           sta 53272
  459.           lda 53265
  460.           ora #64
  461.           sta 53265
  462. main      jsr wordproc
  463.           jsr memodisplay
  464.           jmp main
  465. wordproc  jsr getin
  466.           jsr chrout
  467.           rts
  468. memodsiplay
  469.           ldx #$00
  470. memloop   lda col1,x
  471.           sta 53282
  472.           lda col2,x
  473.           sta 53283
  474.           lda col3,x
  475.           sta 53284
  476.           inx
  477.           cpx #9
  478.           bne memloop
  479.           rts
  480. col1      .byte 6,14,3,7,1,7,3,14,6
  481. col2      .byte 11,12,15,7,1,7,15,12,11
  482. col3      .byte 9,4,8,7,1,7,4,8,9
  483.  
  484.  
  485. Did you have fun playing with the note maker? I did, but it wasn't easy
  486. hitting shift, reverse on, and reverse off. And since this isn't UNIX, we
  487. can actually make it easy to use, so we will!  What we will do is assign
  488. the function key F3 as a reverse on, reverse off switch. So that one tap
  489. and you get your colors, tap again and it will turn off reverse mode.
  490. And you get the other colors if you shift with it on or off. It would be
  491. even better to assign each color a function key, but that will have to
  492. wait.. below is the code you should change..
  493.  
  494.  
  495. wordproc             jsr getin   
  496.                      cmp #134      ;Check for F3
  497.                      beq rvsmode   ;If so then go set rvsmode
  498.                      jsr chrout
  499.                      rts
  500. rvsmode              lda flip1     ;On/Off variable
  501.                      cmp #0        ;If it is off then turn it on
  502.                      bne rvsoff
  503. rvson                lda #18       ;It is off, turn rvs mode on
  504.                      jsr chrout    ;output rvs on
  505.                      lda #1        ;Set our variable to on
  506.                      sta flip1     ;mode 
  507.                      rts           
  508. rvsoff               lda #146      ;It is on, turn rvs mode off
  509.                      jsr chrout    ;output rvs off
  510.                      lda #0        ;set our variable to off mode
  511.                      sta flip1     ;be sure to save it
  512.                      rts
  513.  
  514. flip1               .byte 0
  515.  
  516. Check out the BEQ this means (Branch If Equal) it is the opposite of BNE
  517. (Branch If Not Equal).
  518.  
  519. The above code runs off of the simple concept: if flip1 is on turn it
  520. off, if flip1 is off turn it on. It will always be equal to one or zero.
  521. Sometimes, these little things can get confusing, but a little trial and
  522. error will always help!
  523.  
  524. ___ Power-up Time ____
  525.  
  526. Questions //\\//\\//
  527.  
  528. 1.  Tell what the instructions SEI, CLI, BNE, BEQ, AND, and ORA do.
  529.     (Note we did not truly go into detail on AND/OR, so a general idea
  530.     will do)
  531.  
  532. 2.  What is a monitor? What is an assembler? Compare and contrast.
  533.  
  534. 3.  Can we address more then 256 locations with indexing? Explain how.
  535.  
  536. 4.  Replace the existing .byte statements with different numbers.
  537.     (Check a chart to get the specific colors that you want)
  538.  
  539. 5.  Change the number of colors that the memloop uses. Hint: change the
  540.     cpx #9 and  col1, col2, and col3.
  541.  
  542. 6.  Change the F3 RVS/RVS OFF key to F5 RVS/RVS OFF. Hint: F5 has a
  543.     value of 135.
  544.  
  545. ___Stage Boss___
  546.  
  547.  
  548. Add some text telling how to use the program. Like a simple text
  549. introduction. If you're really brave, add a pop-up help feature. This
  550. could be quite a challenge. You will have to save the contents of the
  551. screen memory, which starts at location $0400, and restore it.
  552.  
  553. Stage #01 Completed.
  554.  
  555. ______Things You've Learned_____
  556.  
  557. Well.. you learned how to code in an assembler. You modified a useful
  558. note program. You learned the most common assembly terms. You increased
  559. your programming skills, and you have a general idea of how the new
  560. instructions work.
  561.  
  562. _____Level #03 Completed_____
  563.  
  564. Sorry to end it up so soon, but we do have deadlines. You can look
  565. forward to the next issue though; we will continue to develop our note
  566. program (the final version will have save, music, edit, and multiple
  567. pages!). We will also look into common assembly problems, and we, of
  568. course, will have a follow up on some of those new mysterious
  569. instructions and much more! In addition we may have some special issues
  570. coming up soon. Well no more delays.. so enjoy and grab another!
  571.  
  572.                                                       The Bonkers Staff
  573.  
  574.