Sharp logo

MZ-800 course Chapter 4 
4. Video-RAM


In this chapter we will discuss the Video-RAM in more detail.

What it this Video-RAM exactly? It is often mentioned at purchase that the SHARP has 16K of Video-RAM available. What is meant by this?

You already know what normal RAM is for, we store our programs there. There is also a storage area which is only used for operations on the screen, this part is called the Video-RAM. Addressing Video-RAM is not as easy as addressing normal RAM. First of all you have to do a bank switch before you can do anything with the Video-RAM. What is bankswitching exactly?

By doing a bank switch you can switch to other memory. Because of the limitations of the Z80-A microprocessor, which can only address 64K of memory, it is necessary to use a different mechanism to address more memory, this can be done with bankswitching. A clever thinker should have figured out that with bankswitching you can address more than 64K. Indeed, it is possible to expand the memory with at least 512K and this extra memory can be addressed with bankswitching. This, however, is just a dream for the future, we are now discussing the Video-RAM, which is only 16K and not 512K.

What does bankswitching look like? You can recognize it if you encounter the instructions DB and E0 ( in mnemonics: IN A,(E0H) ) in the machine language part of a program. After this, the Video-RAM can be found from address 8000 on. Switching back from Video-RAM to normal RAM is done with the instructions dB and E1.

But enough about bankswitching, most of you will probably not understand how it works anyway. The important thing is that you are not kept in the dark about the concept of bankswitching. Later on in this book, some programs from this chapter will be discussed in more detail. Then it will be clear to most of you how addressing Video-RAM is done exactly.

This chapter consists mainly of small programs that show us what can be done with the Video-RAM and this is more than most of you will expect. A note about the expansion ICs making it possible to use 16 colours on your SHARP. With each program it will be mentioned whether it only works with the ICs or on the standard SHARP as well. Sometimes there are some adjustments included to make the result better.


4.1 Scrolling in Video-RAM

Scrolling on the SHARP can be done in two ways: the software- and the hardware- approach. The software approach means that scrolling is done with a program, the hardware approach means that it is done with the internal hardware, you still need a little program to control this hardware. The advantage of the hardware approach is that it is much faster. The disadvantage is that you are bounded to certain rows and that the columns can not be varied. Moreover, scrolling to the side can only be done with big blocks and jumps when using the hardware.

Because side-scrolling can only be done with big jumps, we will not discuss this way of scrolling with the hardware any further.

The following program will show how to use the hardware to scroll ( part of ) the screen. There are some alterations to make the program able to scroll to both sides as well and we will explain how to speed up the scrolling.

Scroll up.

This program will scroll ( part of ) the screen up. Everything that falls off the screen, reappears at the bottom. The program is calibrated in such a way that, when you let the screen scroll exactly one time, the screen will be in exactly the same position as it were before.

We will not explain this program in detail. How the hardware works, is something for hardware producers. You only need to know how to make use of it and with this program and a little explanation, it will hopefully be clear to you.

10 INIT "CRT:M1":INPUT "FROM WHICH LINE ON WOULD YOU LIKE TO SCROLL?
 (MAX. 24) ";A
20 IF A<0 OR A>24 OR A<>INT(A) THEN 10
30 INPUT "UP TO WHICH LINE WOULD YOU LIKE TO SCROLL?
 (MAX. 25)            ";B
40 IF B<=A THEN BEEP:PRINT:PRINT "COULD YOU PLEASE ENTER A HIGHER
 LINENUMBER THAN THE STARTING LINENUMBER?":PRINT:GOTO 30
50 IF B>25 OR B<>INT(B) THEN PRINT:GOTO 30
60 PRINT:INPUT "HOW MANY TIMES WOULD YOU LIKE TO SCROLL A CYCLE?
 (MAX. 20) ";MA
70 IF MA<1 OR MA>20 OR MA<>INT(MA) THEN BEEP:PRINT:GOTO 60
80 LIMIT $FE00
90 POKE $FE00,$DB,$CE,$E6,$40,$20,$FA,$ED,$5B,$33,$FE,$2A,$2E,$FE,
$19,$E5,$ED,$5B,$37,$FE,$B7,$ED,$52,$E1,$20,$3,$2A,$39,$FE,$22,$2E,
$FE,$1,$CF,$6,$21,$32,$FE,
$ED,$BB,$DB,$CE,$E6,$40,$28,$FA,$C9
100 NL=(B-A)*5 :’NL=NUMBER OF LINES
110 BL=A*5 :’BL=BEGIN LINE
120 EL=B*5 :’EL=END LINE
130 S0-(EL-BL)*8
140 S1=INT(S0/256) :S2=S0-S1*256
150 POKE $FE2E,0,0,NL,NL,EL,5,0,0,0,S2,S1,0,0
160 CLS :FOR KL=8 TO 32:CURSOR KL-8,KL-8:PRINT [KL/8,1]"NEPTUNES";:
NEXT KL:CURSOR 0,0:PRINT [1]"NEPTUNES";
170 WAIT 1000:FOR F=1 TO NL/5*8*MA:WAIT 50 /(MA*2.5):USR($FE00):
NEXT F:WAIT 2000 :INIT "CRT:M1"

Unfortunately, this program does not work in all four modes. It works in mode M1 and M2 in any case. For modes M3 and M4 some slight alterations are needed. Since these alterations are not hard to find, we leave it to you to find them.

Scrolling down can be done with the same program with a slight alteration as well. You just have to change the following line:

150 POKE $FE2E,S2,S1,NL,BL,EL,$FB,$FF,0,0,0,0,S2,S1

You have now seen that scrolling with the hardware approach is pretty fast. It can be a lot faster though. This is done by scrolling more than one line at a time, for example two or three rows at a time. The effect is more or less the same, it still looks pretty continuous, it is just somewhat faster. How you can scroll two or three lines at a time, goes as follows:

To scroll up two lines at a time, you have to change the following line:

75 MA=MA/2
150 POKE $FE2E,0,0,NL,BL,EL,10,0,0,0,S2,S1,0,0

To scroll up three lines at a time, you have to change the following line:

75 MA=MA/3
150 POKE $FE2E,0,0,NL,BL,EL,20,0,0,0,S2,S1,0,0

To scroll down two lines at a time, you have to change the following line:

75 MA=MA/2
150 POKE $FE2E,S2,S1,NL,BL,EL,$F6,$FF,0,0,0,0,S2,S1

To scroll down three lines at a time, you have to change the following line:

75 MA=MA/3
150 POKE $FE2E,S2,S1,NL,BL,EL,$EC,$FF,0,0,0,0,S2,S1

You can go on like this forever, but at a certain point the effect is not as great as it used to be. The screen will move up or down in big blocks and that is hardly what we want.

On the next pages the software approach for scrolling will be discussed. That way, a lot more possibilities are open to us and they will all be explored. You shall also see that scrolling this way is far more flexible, but it is a lot slower.

Scrolling with the software approach.

Scrolling with the software approach is far more flexible. You can scroll up, down, to the left, to the right and numerous variations are possible. For example; you can make the screen disappear at the end, but you can also put it back at the beginning. This way you can also scroll something on the screen that is stored in the memory somewhere. You can scroll part of the colours and leave the rest in tact, but you can also scroll all colours. There are numerous possibilities and we shall discuss all of them.

Program 1: Scrolling to the left 1 byte at a time in 1 colour.

The simplest form of scrolling is scrolling the complete screen in 1 colour, in 1 direction and with blocks of 8 bits ( 1 byte ) at a time. After this program we shall go into this further.

10 LIMIT $FE00
20 DATA $DB,$E0,$3E,$83,$D3,$CC,$D3,$CD,$21,$00,$80,$11,$01,$80,$0E,
$C8,$7E,$08,$06,$27,$1A,$77,$23,$13,$10,$FA,$08,$77,$23,$13,$0D,$20,
$EF,$DB,$E1,$C9
30 FOR A=0 TO 35:READ B:POKE $FE00+A,B:NEXT A
40 INIT "CRT:M1"
50 LIST:LIST
60 FOR A=1 TO 80:USR($FE00):NEXT A

This program will make everything that falls of the screen at the left side, reappear at the right side. So the program can be made simpler by just letting everything at the left side disappear, but then the effect is not as good.

It is very simple to scroll the colour blue or red for example, leaving another colour. This can be done because the colour white is composed of all other colours. Just add the following line to the program and watch the result:

35 POKE $FE03,$1

You shall see that the colour blue is scrolled and that the colour red stays behind and on the places where red and blue intersect, the colour white can be seen again. The opposite ( scrolling red and leaving blue ) can be done by changing line 35 to:

35 POKE $FE03,$2

This phenomenon shall return several times this chapter. There the possibilities in mode M2 shall be discussed as well, in this mode you can use 16 colours at the same time. By scrolling in mode M2, it can be shown that the 16 colours are actually composed of 4 primary colours. This implies that all 16 colours are scrolled when the 4 primary colours are scrolled. This will be discussed later on in more detail.

Program 2: Scrolling up in 1 colour.

The program for scrolling up is shorter than the one for scrolling sideward and it is also a bit simpler.

10 INIT "CRT:M1":LIMIT $FE00
20 DATA $DB,$E0,$3E,$83,$D3,$CC,$D3,$CD,$21,$00,$80,$11,$D8,$7F,$01,
$40,$1F,$ED,$B0,$11,$18,$9F,$21,$D8,$7F,$01,$28,$00,$ED,$B0,$DB,$E1,
$C9
30 FOR A=0 TO 32:READ B:POKE $FE00+A,B:NEXT A
40 LIST:LIST:LIST
50 FOR T=1 TO 200:USR($FE00):NEXT T
60 GET A$:IF A$="" THEN 60
70 CLS:END

From these two programs it is easy do deduce how scrolling to the left and down is done, only a couple of small changes are needed.

Of course it is not mandatory to scroll the complete screen each time. Scrolling part of the screen is allowed as well. At the end of this part of chapter 4 some examples will be given that scroll part of the screen.

Program 3: Mixing red and blue in mode M1.

To prove that in mode M1 the colours blue and red combined yield the colour white, here is a program that shows just that.

The program moves two blocks in the colours blue and red towards each other and sometimes lets part of these two block overlap. The overlapping area will become white.

10 LIMIT $FD00
20 DATA $DB,$E0,$3E,$01,$D3,$CC,$D3,$CD,$21,$3F,$9F,$11,$3E,$9F,$0E,
$C8,$7E,$08,$06,$27,$1A,$77,$2B,$1B,$10,$FA,$08,$77,$2B,$1B,$0D,$20,
$EF,$DB,$E1,$C9
30 DATA $DB,$E0,$3E,$02,$D3,$CC,$D3,$CD,$21,$00,$80,$11,$01,$80,$0E,
$C8,$7E,$08,$06,$27,$1A,$77,$23,$13,$10,$FA,$08,$77,$23,$13,$0D,$20,
$EF,$DB,$E1,$C9
40 FOR A=0 TO 35:READ B:POKE $FD00+A,B:NEXT A
50 FOR A=0 TO 35:READ B:POKE $FD30+A,B:NEXT A
60 INIT "CRT:M1"
70 BOX [1]0,0,100,125,1
80 BOX [2]219,75,319,199,2
90 FOR A=1 TO 80
100 USR($FD00):USR($FD30):NEXT A
110 GET A$:IF A$="" THEN 110
120 CLS:END

Program 4: Scrolling all 16 colours independently in mode M2.

The previous three programs work on all SHARPs. For this program however, you need the expansion-ICs.

As you know, you can use 16 colours at the same time in mode M2. It is possible to scroll each of these colours separately. The disadvantage is that all other colours disappear.

10 INIT "CRT:M2":LIMIT $FE00
20 DATA $DB,$E0,$3E,$81,$D3,$CC,$D3,$CD,$21,$00,$80,$11,$D8,$7F,$01,
$40,$1F,$ED,$B0,$11,$18,$9F,$21,$D8,$7F,$01,$28,$00,$ED,$B0,$DB,$E1,
$C9
30 FOR A=0 TO 32:READ B:POKE $FE00+A,B:NEXT A
40 FOR D=1 TO 15
50 FOR A=1 TO 15:BOX [A](A-1)*20,92,A*20-2,180,A:NEXT A
60 WAIT 2000:POKE $FE03,160+D
70 FOR T=1 TO 200:USR($FE00):NEXT T,D
80 GET A$:IF A$="" THEN 80
90 CLS:END

Program 5: Scrolling all 4 primary colours independently in mode M2

It is also possible to scroll only one of the primary colours. This can be done by making a small adjustment to program 4. This time all other colours will remain the same. You still need the extra ICs for this. The following alterations are needed:

40 D=1
60 POKE $FE03,D
70 FOR T=1 TO 200:USR($FE00):NEXT T
75 IF D<8 THEN D=D*2:GOTO 60

By studying this program you can see exactly from which primary colours the 16 colours consist.

Program 6: Scrolling all 4 primary colours at once in mode M2.

Of course it is also possible to scroll all four primary colours at once. For convenience we have implemented it in the BASIC-part of the program this time. It is not as fast as it would be in machine code, as shall be shown later on.

You need the following alterations in program 4:

DELETE 40
60 FOR T=1 TO 2000:D=1
70 POKE $FE03,D:USR($FE00):D=D*2:IF D<16 THEN 70
75 NEXT T

Program 7: A nice demo.

This program shows once more that we can do nice things in a simple way with scrolling.

In this program a screen is loaded in memory, after which that screen appears bit by bit on another screen and disappears again in a nice way.

You need the extra ICs again for this program.

10 LIMIT $DE00
20 DATA $DB,$E0,$21,$00,$80,$11,$00,$DE,$01,$00,$20,$ED,$B0,$DB,$E1,
$C9
30 DATA $DB,$E0,$3E,$85,$D3,$CC,$D3,$CD,$21,$F0,$9E,$11,$00,$DE,$0E,
$01,$06,$50,$1A,$77,$23,$13,$10,$FA,$0D,$20,$F5,$DB,$E1,$C9
40 DATA $DB,$E0,$21,$00,$80,$11,$01,$80,$0E,$32,$06,$50,$1A,$77,$23,
$13,$10,$FA,$36,$00,$3E,$50,$13,$23,$3D,$20,$FB,$0D,$20,$EC,$DB,$E1,
$C9
50 DATA $DB,$E0,$21,$3F,$9F,$11,$3E,$9F,$0E,$32,$06,$50,$1A,$77,$2B,
$1B,$10,$FA,$36,$00,$3E,$50,$1B,$2B,$3D,$20,$FB,$0D,$20,$EC,$DB,$E1,
$C9
60 FOR A=0 TO 15 :READ B :POKE $FE00+A,B:NEXT
70 FOR A=0 TO 29 :READ B :POKE $FE80+A,B:NEXT
80 FOR A=0 TO 32 :READ B :POKE $FEA0+A,B:NEXT
90 FOR A=0 TO 32 :READ B :POKE $FED0+A,B:NEXT
100 INIT "CRT:M4" :CURSOR 0,24 :PRINT " ";:PAL 3,0 :GH=0
110 SYMBOL 98,0,"THIS IS ALSO A",4,2:
SYMBOL 66,16,"POSSIBILITY WITH",4,2
120 SYMBOL 194,32,"SCROLLING",4,2
:SYMBOL 130,48,"made by",4,2
130 SYMBOL 86,66,"NEPTUNES PRODUCTIONS",3,4:CURSOR 0,0:PRINT " "
140 USR($FE00):INIT "CRT:M4":PAL 0,1:PAL 1,0:PAL 2,7:PAL 3,14
150 BOX [2]0,100,639,199,2:B=3:A=100
160 LINE [1]0,A,640,A:A=A+B:B=B+1:IF A<200 THEN 160
170 B=0:FOR A=160 TO 0 STEP -10:LINE [1]A*2,100,2*(A-B),200
180 LINE [1]640-2*A,100,640-2*A+2*B,200:B=B+30:NEXT A
190 F=40688:C=2
200 USR($FE80):GH=GH+1:F=F-80:H=INT(F/256):G=((F/256)-H)*256
210 POKE $FE8A,H:POKE $FE89,G:POKE $FE8F,C:C=C+1:IF C=101 THEN 230
220 WAIT 100-C:GOTO 200
230 FOR A=1 TO 80:USR($FEA0):USR(FED0):GH=GH+1:NEXT A
240 GET A$:IF A$="" THEN 240
250 INIT "CRT:M1":END

Of course this program can also be faster and probably even shorter by converting some BASIC parts of the program into machine code and by compressing the machine code part, because it is all a bit digressive. The advantage of this digressiveness is that the machine code part is more readable and therefore easier to understand, because very few complicated machine code instructions are used.

Program 8: Scrolling left bit by bit with 1 primary colour.

In principle, this scrolling technique is equal to the byte by byte scrolling. The only difference is that scrolling bit by bit goes more smoothly and therefore the result is better.

10 INIT "CRT:M1"
20 FOR A=1 TO 3:BOX [A]140,A*30,180,(A+1)*30-4,A:NEXT A:WAIT 2000
30 POKE $FD00,$DB,$E0,$3E,$1,$D3,$CC,$D3,$CD,$21,$17,$9F,$D9,$21,$F0,
$9E,$E,$C7,$7E,$1F,$D9,$6,$28,$CB,$1E,$2B,$10,$FB,$D9,$11,$D8,$FF,
$19,$D,$20,$EE,$DB,$E1,$C9
40 FOR A=1 TO 320:USR($FD00):NEXT A
50 GET A$:IF A$="" THEN 50
60 CLS:END

Of course we can flip the colours by adding the following line:

35 POKE $FD03,$2

By the way: it does not matter if you change the colour of palettes 1 or 2, because the new colour will become a primary colour.

A weird situation arises when you give palettes 1 and 2 the same colour, just try it by adding the following line:

15 PAL 1,6:PAL 2,6

Now you can see that white consists of two equal colours. So now you might understand that mixing colours in mode M1 is done in a very weird way, which unfortunately, we can not explain to you.

This program also shows that scrolling bit by bit is slower than scrolling byte by byte and that this program is more complicated, but on the other hand this gives a better result.

Program 9: Scrolling all colours at once.

This program only works with the expansion ICs. Scrolling in mode M1 goes in a similar way. In mode M1 only the first two primary colours need to be scrolled and in mode M2 all four primary colours need to be scrolled.

Scrolling the four primary colours successively is done exclusively in machine code in this program and it is done 320 times ( to end in the starting position ), which makes it somewhat faster.

It is also possible to scroll 1 of the 16 colours separately or 1 primary colour at a time when using bit by bit scrolling, a couple of small adjustments are needed for the following program, which you can probably figure our for yourself after all the examples.

10 INIT "CRT:M2"
20 CLS:KH=2:FOR T=0 TO 7:FOR U=0 TO 9:CURSOR U*4,T*3:
PRINT [KH]CHR$(200);:KH=KH+1:IF KH=16 THEN KH=1
30 NEXT U,T
40 POKE $FD00,$DB,$E0,$3E,$1,$D3,$CC,$D3,$CD,$21,$17,$9F,$D9,$21,
$F0,$9E,$E,$C7,$7E,$1F,$D9,$6,$28
50 POKE $FD16,$CB,$1E,$2B,$10,$FB,$D9,$11,$D8,$FF,$19,$D,$20,$EE,
$3A,$3,$FD,$87,$32
60 POKE $FD28,$3,$FD,$FE,$10,$C2,$2,$FD,$3E,$1,$32,$3,$FD,$1,$40,
$2,$D,$ED,$43,$35,$FD
70 POKE $FD3C,$20,$C4,$5,$ED,$43,$35,$FD,$20,$BD,$DB,$E1,$C9
80 USR($FD00)
90 GET A$:IF A$="" THEN 90
100 CLS:END

Program 10: Tunnel.

In this program only part of the screen will be scrolled and in such a way that a tunnel will appear. This can be implemented
in a game easily. We use it in the game TUNNEL.

This program works on any SHARP MZ-800.

10 INIT "CRT:M1":Y=81
20 BOX 88,59,231,122,3
30 POKE $FD00,$DB,$E0,$3E,$2,$D3,$CC,$D3,$CD,$21,$2C,$93,$E,$40,
$37,$3F,$6,$12
40 POKE $FD11,$CB,$1E,$2B,$10,$FB,$11,$EA,$FF,$19,$D,$20,$F0,$DB,
$E1,$C9
50 LINE [3]231,Y,231,Y+20:USR($FD00)
60 IF RND>=.5 AND Y<100 THEN Y=Y+1
70 IF RND<.5 AND Y>62 THEN Y=Y-1
80 GOTO 50

Program 11: Scrolling a part up.

Scrolling part of the screen up is not as easy as scrolling the whole screen up. The machine code part is a little bit larger, but the principle is the same.

The part that will be scrolled gets another colour than the surroundings to accentuate the scrolled part. There also is a small delay, because otherwise the scrolling would be to fast and the result would not be as good.

10 INIT "CRT:M1":LIMIT $FE00:PAL 2,9
20 DATA $DB,$E0,$3E,$82,$D3,$CC,$3E,$02,$D3,$CD,$21,$D8,$7F,$11,
$00,$88,$06,$14,$1A,$77,$23,$13,$10,$FA,$21,$00,$88,$11,$28,$88,
$0E,$30,$06,$14,$1A,$77,$23,$13,$10,$FA,$06,$14,$23,$13,$10,$FC,
$0D,$20,$EF
30 DATA $11,$D8,$7F,$06,$14,$1A,$77,$23,$13,$10,$FA,$DB,$E1,$C9
40 FOR A=0 TO 32:READ B:POKE $FE00+A,B:NEXT A
50 LIST:LIST:WAIT 2000
60 FOR T=1 TO 147:USR($FE00):WAIT 10:NEXT T:WAIT 1000:PAL 2,15
70 GET A$:IF A$="" THEN 70
80 CLS:END

These were two programs that should have clarified how to scroll part of the screen. At the same time the second program showed that the part that is scrolled can be given another colour. Now we will look at another nice program that shows what the effect is when the whole screen is scrolled up and when the bottom part is also scrolled to the left. This will also be the last program about scrolling in Video-RAM. How you can access the Video-RAM from the ROM-monitor will be discussed further on in this chapter.

Program 12: Funny joke.

10 INIT "CRT:M1":LIMIT $FD00
20 POKE $FD00,$DB,$E0,$3E,$83,$D3,$CC,$D3,$CD,$21,$17,$9F,$D3,$21,
$F0,$9E,$E,$63,$7E,$1F,$D9,$6,$28,$CB,$1E,$2B,$10,$FB,$D9,$11,$D8,
$FF,$19,$D,$20,$EE,$DB,$E1,$C9
30 DATA $CB,$E0,$3E,$83,$D3,$CC,$D3,$CD,$21,$00,$80,$11,$D8,$7F,$01,
$40,$1F,$ED,$B0,$11,$18,$9F,$21,$D8,$7F,$01,$28,$00,$ED,$B0,$DB,$E1,
$C9
40 FOR A=0 TO 32:READ B:POKE $FE00+A,B:NEXT A
50 LIST:LIST
60 FOR A=1 TO 200:USR($FD00):USR($FE00):NEXT A 

Previous page
Next page
Contents


Go to the top of this page Home

last updated July 5, 2004
Arjan Habing, Mark de Rover, Jeroen F. J. Laros, sharpmz@sharpmz.org