ASM 6809: les variables

Cette catégorie traite de développements récents pour nos vieilles machines, applications, jeux ou démos... Amis programmeurs, c'est ici que vous pourrez enfin devenir célèbres!

Modérateurs : Papy.G, fneck, Carl

Répondre
Garland_Raven
Messages : 25
Inscription : 19 juin 2017 19:00

ASM 6809: les variables

Message par Garland_Raven »

First of all: feel free to answer me in french. I can fully understand it but im not so good on writing it, thats why i prefer to use english. :)

I was learning some 6809 and i have one little question. It's about the location of a character but its generally focused on the concept of variables.

Lets take this code:

Code : Tout sélectionner

 FCB $1F, $4C, $41
It sets the character location to coordinates specified after the US code $1F (so, $4C and $41).

So everytime the program reads this, it sets the next character location.

My purpose is to have that couple of values changing dynamically. I would store those two values in two memory cells, then use it to assign the values as $1F values. It is possibile? What syntax i have to use?

Thank you guys :)
Dernière modification par Garland_Raven le 20 juin 2017 20:35, modifié 1 fois.
Daniel
Messages : 17316
Inscription : 01 mai 2007 18:30
Localisation : Vaucluse
Contact :

Re: ASM 6809: les variables

Message par Daniel »

Attention : je ne connais pas la syntaxe exacte pour Assembler v3.6, il faut peut-être faire quelques modifications.
Ce code permet d'afficher une chaîne de caractères à la position spécifiée.

Code : Tout sélectionner

....
STRING
  FCB  $1F,$00,$00,.......printable characters....., $04          ; $04 is the end of string character

....

; ----------------------------------
; PRINT $04 terminated string
; ----------------------------------
  LDD  #$4C41       ; location
  STD  STRING+1     ; update location sequence
  LEAX ,STRING      ; pointer to string
PRINT
  LDB  ,X+          ; get string byte
  CMPB #$04         ; test end of string
  BEQ  ENDPRINT     ; done                          
  SWI               ; function call
  FCB  $02          ; print character
  BRA  PRINT        ; next byte
ENDPRINT

.....
Daniel
L'obstacle augmente mon ardeur.
Fool-DupleX
Messages : 2284
Inscription : 06 avr. 2009 12:07

Re: ASM 6809: les variables

Message par Fool-DupleX »

Depending on what you want to do, the approach of using PUTCH$ (call $02) might be too slow. However to modify the values dynamically, you could do:

Code : Tout sélectionner

STRING
  FCB   $1F, ...

...
  LDA   #your_value
  STA   STRING+1
  LDA   #your_value
  STA   STRING+2
or put a bit more nicely :

Code : Tout sélectionner

STRING  FCB   $1F
VAL1    FCB   $00
VAL2    FCB   $00
        FCB ... printable characters ... $04

...
        LDA   #your_value 
        STA   VAL1
        LDA   #your_value
        STA   VAL2

Which is the same as Daniel proposed, put in a simpler, more naive form. The important thing is the #, which means this is a value for immediate use. Without the #, whatever comes next to the operand is considered an address in memory. LDA #$23 loads the value $23 in A, whereas LDA $23 will fetch the value stored at address $0023. It's the main source of confusion for beginners and I admit still making the mistake form time to time, it's so easy to forget the #.
Garland_Raven
Messages : 25
Inscription : 19 juin 2017 19:00

Re: ASM 6809: les variables

Message par Garland_Raven »

Thank you both guys :) it worked !!

If i got this right, the only way to swap values between cells is always by loading and getting from the accumulator (no MOV like instructions where you can put directly a value in a specific address).

So now what i'm trying to do is to move a character left and right by keyboard. I read about the 0C address (KTSTH) and i would like do something like:

Code : Tout sélectionner

LDU DEFCHR

..

KEY  CALL 0C .ktsth
     BCS CHK
     BRA DRAW

CHK CALL 0A   .getchar
    CMPA #$9  .right key
    BQE RIGHT
    CMPA #$8  .left key
    BQE LEFT
    BRA DRAW
   
RIGHT LDA COLUMN
      INCA
      STA COLUMN
LEFT  LDA COLUMN
      DECA
      STA COLUMN
DRAW  LDU DEFPOS

..

DEFCHR ...
DEFPOS  FCB $1F
LINE    FCB $00
COLUMN  FCB $00  
...... $04
I dont think this is really the good way to accomplish the job, so any suggestion is much appreciated.

Thank you so much guys :)
Fool-DupleX
Messages : 2284
Inscription : 06 avr. 2009 12:07

Re: ASM 6809: les variables

Message par Fool-DupleX »

Yes, there's a lot of room for improvement here, but you're learning, so that's ok.

On the 6809, you must always use the accumulators to do operations on the data, including moving it. However, a few instructions have extended operation and INC is one of them so you can do:

Code : Tout sélectionner

INC >COLUMN
that will work and it's already a nice improvement over your code. Sam, one of our uber-coders on Thomson, is keen on optimizing code, but his solutions are usually so clever that you don't understand the code anymore, at least not at first sight. :wink:

The more you work with the 6809, the more you'll realize that each instruction has a particular set of things it can do and that's on purpose because the processor was designed with particular goals in mind.

Note that there's a BRA missing in between your LEFT and RIGHT sections.
__sam__
Messages : 7923
Inscription : 18 sept. 2010 12:08
Localisation : Brest et parfois les Flandres

Re: ASM 6809: les variables

Message par __sam__ »

Note that there's a BRA missing in between your LEFT and RIGHT sections.
Is that called topless coding ? :P (missing bras)

/me having a weird sense of humour. It's way too hot for me where I am. Sorry guys.. I need to take a little nap.
Samuel.
A500 Vampire V2+ ^8^, A1200 (030@50mhz/fpu/64mb/cf 8go),
A500 GVP530(MMU/FPU) h.s., R-Pi, TO9, TO8D, TO8.Démos
Garland_Raven
Messages : 25
Inscription : 19 juin 2017 19:00

Re: ASM 6809: les variables

Message par Garland_Raven »

__sam__ a écrit :
Note that there's a BRA missing in between your LEFT and RIGHT sections.
Is that called topless coding ? :P (missing bras)

LOL :)

Thanks for all guys !! I'm loving ASM ♥

I spent last night working more on my code. Sorry for being a little annoying but i have a couple of new questions:

1) I don't get how exactly registers X and Y are working. As far as i got, if you load with LDX and LDY in X and Y registers you're setting the coordinates for the next character which you will draw with STB CHDRAW then CALL CHPLH. However, i don't understand statements like STA 0,X or LEAX -319,X - i know that STA puts the accumulator value into 0,X but is this meaning "location 0 of X register" ? So LEAX loads from effective address -319 of X ? Sure im a little confused but i thinks its normal for a beginner :)

2) I'm using KTSTH and GETCH to read keyboard input, it is the fastest way to do it? Also, i set a LDA #0, STA $2076 to erase the keyboard latency, it is the best way to get immediate response?

3) I'd need to erase the old character position before to draw the new one. So at the moment i'm following a very rookie way: the adresses $2032 and $2034 contains the position of the last character written. I'm referring these as PLOTX and PLOTY, so to erase i do:

Code : Tout sélectionner

LDX PLOTX
LDX PLOTY
LDB #$20
STB CHDRAW
CALL CHPLH
This writes a space character on the old position. But i think it is a very rookie way to do it. Also, i need SPEED, so my goal is to get the fastest way to accomplish the erase/draw task.
On the book there's an example (inner page 152 or page 162 on djvu GUI) which loads in X the address of first octect (&3840) then copy it to 1,X to "move" it, then puts 0 in 0, X to erase the old one. But i don't really get how this is working, so if you can give me a more detailed explication i'd be grateful :)

4) Using characters, im limited to 2 colors x 8x8 cell.. so this means i can only set two colors (back and fore) and its pretty common for the 8bits. But what if would mask some bits? Let's say i dont want to show a specific pixel in a 8x8 grid, for example the character draws a ball and i don't want to see the square box around it. There should be something to make a bitblittering or like so, do you know how can i get it?

Overall : as you can easily get, my final goal here is to make a game. I worked so much with basic on Pc128 when i was a kid but i always liked to do some "arcade-like" game using ASM. I saw horrible games on this machine, but there are also excellent games like Avenger or Vampire, so i would know how much far you can go pushing the hardware. Vampire is shockingly FAST and so was Androides (big Hennebois fan here!) im sure this machine can give you so much satisfaction even without the VIC II or the SID if you're good enough to program it :)

Sorry for the wall of text :) feel free to answer me whenever you want :)

Thanks again!
__sam__
Messages : 7923
Inscription : 18 sept. 2010 12:08
Localisation : Brest et parfois les Flandres

Re: ASM 6809: les variables

Message par __sam__ »

0,x stands for adress x+0. so sta 0,x stores the accumultor at x+0.

lea effectively stands for load the effective address. leax -319,x loads in x the address x-319. in fact this is similar to x=x-319 in c/basic/etc.

you can do the same with all address registers. for instance leau -2,y is the same as u:=y-2.

there is also a nice trick when the destination is one of the x or y register: the z flag of the condition register is set. this way you can easily build fast loops with 16 bits registers.

oh sorry it seem my keyboard has lost its uppercase. :-/
Samuel.
A500 Vampire V2+ ^8^, A1200 (030@50mhz/fpu/64mb/cf 8go),
A500 GVP530(MMU/FPU) h.s., R-Pi, TO9, TO8D, TO8.Démos
Garland_Raven
Messages : 25
Inscription : 19 juin 2017 19:00

Re: ASM 6809: les variables

Message par Garland_Raven »

__sam__ a écrit : lea effectively stands for load the effective address. leax -319,x loads in x the address x-319. in fact this is similar to x=x-319 in c/basic/etc.
While learning the syntax and the functions is one part of the job, the most difficult part is to get how to organize the code: i know it might be a silly question, but is there a relationship between the X resolution of the screen (320) and the -319 address of X ? I was following this example which is referring to a moving cursor and it's not easy to understand why it uses specific addresses, like in -319,x in this case.
http://dcmoto.free.fr/documentation/pas ... ement.djvu
page 147

Thanks 8)
__sam__
Messages : 7923
Inscription : 18 sept. 2010 12:08
Localisation : Brest et parfois les Flandres

Re: ASM 6809: les variables

Message par __sam__ »

i haven't read the code but it is likely that x contains a kind of pixels number in linear order. x-319 then refers to the pixel in upper right position from the one designated by x. notice this is just hypothetical. further inspection of the code will give more hints.
Samuel.
A500 Vampire V2+ ^8^, A1200 (030@50mhz/fpu/64mb/cf 8go),
A500 GVP530(MMU/FPU) h.s., R-Pi, TO9, TO8D, TO8.Démos
Garland_Raven
Messages : 25
Inscription : 19 juin 2017 19:00

Re: ASM 6809: les variables

Message par Garland_Raven »

Image

So if i got it right, 40 addresses are because there are 40 columns in the text mode, then to grab the upper or lower octet i must offset of 40 addresses. 320 then is 40*8 raws which are composing the character, so 1,X means near one pixel right and -1,X one pixel left.
Now i was wondering: why putting 0 in 0,X erases the char and why the entry of the first octet is &3840? Is that address referred to the starting location on the screen ($1F, $4C, $41)?

Thanks!

this is the referred code:

Image
Fool-DupleX
Messages : 2284
Inscription : 06 avr. 2009 12:07

Re: ASM 6809: les variables

Message par Fool-DupleX »

I remember that program, I tried it when I was something like 12. Lots of questions here !

Actually you are missing the point. There are two distinct parts dealing with display. The initialisation part uses PUTCH to draw a target and the ball. But for the scroll, the pixels are manipulated directly in the video RAM, not with PUTCH. PUTCH is very slow, you can't use that for decent animations. You have to do everything yourself in the video ram, and that's what this program does.

Also, there is no text mode on Thomson. It's only graphic, text is actually emulated by PUTCH. I remember that I was shocked when I first used a PC (was about 14). I could not understand why it was not possible to draw a line and that you had to switch to this weird CGA graphic mode first, to be able to draw a line.

The 320 is actually not related to the resolution of the screen but to the fact that the ball is 8x8 pixels. 8 pixels are defined by one byte and there are 40 bytes per line (40x8 = 320). Every 40 bytes, you are on the next line in the video. Since the ball is 8 pixels high, the distance in RAM between the first and the last segment of 8 is 40x8, hence 320 bytes. When the program copies the ball to the next column, it has to take one byte every 40 bytes 8 times. The adress is therefore incremented by 40 until 320 is reached.

If you want to understand this program, you must first understand how the video works on Thomson. It's key.

Now for your other questions:

1. X and Y are general purpose index registers, by opposition to A and B which are accumulators. They work differently. There's a whole set of instructions and addressing modes dedicated to them. X and Y are most of the time used to fetch and store data from/to the memory. For example :

Code : Tout sélectionner

LDD ,X++
is a pretty powerful statement. It loads 2 bytes of data from the adresses at X and X+1 in A and B, then increments the X pointer by 2. All that in a single instruction!

Don't make the mistake to think X and Y are accumulators or coordinates or whatever. Using X and Y as inputs when calling DRAWH is a mere convention that was decided by the guy who wrote the DRAWH routine.

2. Using the monitor routines such as KTSTH or DRAWH is for sure NOT the fastest way. It's actually awfully slow. Think of these routines as a kind of very simple kernel with simple drivers, well actually, you call that a monitor. Monitors were what was before kernels. These routines are here for compatibility (more or less identical on all Thomson machines) and to simplify a programmer's work.

There are three reasons why these monitor routines are slow :

a) they are generic and designed to do a lot of things. PUTCH is a kind of printf. It's huge. Freaky awesome, you'll love it. Believe me. But it's so slow.
b) to call them you actually invoke an interrupt, a bit like INT 21H in DOS or INT 80H in Linux. And that's slow, oh yes it is. Believe me, it's true.
c) the interrupt does a lot of other things before actually executing the routine you call. Things that you don't need, billions of stolen CPU cycles, total disaster.

So if you want to make your PC128 great again, you must do everything yourself and that means poking that video RAM damn hard and scrutinize the keyboard key per key. (hint: you may even scrutinize only the keys you need instead of the whole keyboard).

But you are too young for that. It's better to understand the various examples given in the book first.

This whole story is interesting because it tells a lot about why some software on Thomson were so awful and some were just unbelievable. 70-80% of the commercial software would use BASIC. BASIC is slooooooooooooooooow. 15% of the software would use assembly and the monitor, well that's much faster but still ... 5% of the software, the really good software, would use assembly and control every aspect of the electronics themselves, without the monitor. If all the software on Thomson were like these 5%, it would have been something, I can tell you.

Think of our OS-9 project (http://os9.forler.ch, website totally outdated but project still up), it's crazy compared to Microsoft BASIC.
Garland_Raven
Messages : 25
Inscription : 19 juin 2017 19:00

Re: ASM 6809: les variables

Message par Garland_Raven »

First of all, this answer melts face and im so grateful to you man :) one of the reasons that made me to learn some ASM is the admiration for such all low-ended coders like you. When i was 12, finding a book like this one, or finding some deep notions about 6809 ASM and the architecture of MO5 was really hard for a kid, especially in Italy. There were tons of magazines and books but they were always filled with BASIC so kids like me never got the opportunity to work with ASM and they were "forced" to use BASIC. Next jump was C/C++ but the desire of learning ASM was always been here.


Sure, i need to learn a little bit more and i'll take my time to have a good progressive curve.

You see, sometimes you have to ask senors for not losing yourself in a dead end. I was sure that routines like KTSTH and GETCH were fast because... well it's ASM, and these are the usual routines that are offered to you by the books (i took a look to ASSDESASS doc and it tells you about KTSTH and GETCH too like the only way to read keyboard as well). Didn't knew using monitor was SLOW :D



I think i'll take a look to PIA registers ($A7C1) for the keyboard and also at the GETCH source code to get only what i need ;) if you have further suggestions i'm all ears.

http://os9.forler.ch/ is a wonderful project and i wish you all the best for it :)
Fool-DupleX
Messages : 2284
Inscription : 06 avr. 2009 12:07

Re: ASM 6809: les variables

Message par Fool-DupleX »

Well if you want my opinion it's certainly more useful to learn a modern machine code like ARM or Atmel. But I must admit I don't get the same fun from coding ARM rather than 6809. Funny also to see that what was quite commonplace back in the days due to the constraints in RAM and speed is today known as "bare metal programming", considered totally mindblowing and utterly respected.

The book you use is really very progressive and you should really take the time to understand each chapter. The ball game is already something advanced in the book. The book unfortunately misses many of the features of the PC128, like all the various graphic modes, because the machine came to the market just about when the book was released. On the contrary, there are some very interesting chapters at the end like displaying numbers (not that easy) or sort algorithms.

We are a few to know all these machines by heart : Sam, Daniel, jb_jb_fr, Prehisto are the people with whom I talk most but there are more, apologies for not citing them all.

Oh, by the way, the OS-9 project has been ported to the MO6/PC128, if you wanna try that, MP me.
Garland_Raven
Messages : 25
Inscription : 19 juin 2017 19:00

Re: ASM 6809: les variables

Message par Garland_Raven »

I'm already a full time programmer on high-ended languages for all platforms so using Visual Studio or XCode is my daily job and as you know today is easier to code performant software thanks to modern hardware speed, engines, tools, and so on ;) i wish to learn 6809 ONLY because since i was kid i wanted to code an arcade game (not with basic) on my PC128. That would be a homework for summer, nothing more, i don't have to turn it into business ;) I'd like to code for MO6 (which is the Pc128) but as you said i couldn't find a book with the specific features for that machine so MO5 at the moment is enough to me. Yes i'm MP you !! :wink:
Répondre