News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

Example Z80 assembly programs (was:ASM source code)

Started by arnoldemu, 08:59, 04 April 10

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

funkheld

#175
FutureOS ASM , a nice quick thing :

Use the Q/W/E/S keys to move the "A" over the graphic.
Every time you move the "A" the image is updated very quickly without flickering.
It stops at the edges.

Use the "a" key to exit the program.

greeting

------------------------------------------
ORG &8000

; mode 1
LD  A,(RAMCHAR)
SET 0,A
RES 1,A
LD  (RAMCHAR),A
LD  B,&7F
OUT (C),A

LD IX,LESC
CALL ROM_A2C
                                 
LD  HL,S80X25
CALL ROM_A

LD  BC,&7F10 ;border
OUT (C),C
LD  A,&55   
OUT (C),A   
LD  (BORDER),A  ;border color &40-&5F

LD  BC,&7F00 ;INK 0
OUT (C),C
LD  A,&54     
OUT (C),A
LD  (INK_0),A ;paper color &40-&5F

LD  BC,&7F01 ;INK 1
OUT (C),C
LD  A,&52   
OUT (C),A
LD  (INK_1),A ;INK 1 color &40-&5F

LD  BC,&7F02 ;INK 2
OUT (C),C
LD  A,&4f   
OUT (C),A
LD  (INK_2),A ;INK 2 color &40-&5F

LD  BC,&7F03 ;INK 3
OUT (C),C
LD  A,&4c     
OUT (C),A
LD  (INK_3),A ;INK 3 color &40-&5F

LD A,(REG_PC+1)
ld a,0
LD (MEDIUM),A

;Lade Bild in E-RAM
LD BC,&7fc4 
LD (AKT_RAM),BC
LD  DE,LBI
CALL LABIER

anfang:

schleife:
call H_ALLET
jr z,schleife

ld (hl),a
cp "q"
jp nz,weiter1

ld  a,(TXT+4)
dec a
cp &ff
jr nz,mache
jr gehe

mache:
ld (TXT+4),a
call posxy

gehe:
jp anfang

weiter1:
ld (hl),a
cp "e"
jr nz,weiter2

ld  a,(TXT+4)
inc a
cp 40
jr nz,mache1
jr gehe1

mache1:
ld (TXT+4),a
call posxy

gehe1:
jp anfang

weiter2:
ld (hl),a
cp "w"
jr nz,weiter3

ld  a,(TXT+3)
dec a
cp &ff
jr nz,mache2
jr gehe2

mache2:
ld (TXT+3),a
call posxy

gehe2:
jp anfang

weiter3:
ld (hl),a
cp "s"
jr nz,weiter4

ld  a,(TXT+3)
inc a
cp 25
jr nz,mache3
jr gehe3

mache3:
ld (TXT+3),a
call posxy
jr gehe3

gehe3:
jp anfang

weiter4:
  ld (hl),a
  cp "a"
  jp nz,anfang
  call ende

TXT DB &02,&10, &1F,10,10,"A",&00

LBI DB &00,"BILDF001SCR"
MEDIUM DB &00

ende:
LD HL,TUR_E
JP ROM_D 
ret

posxy:
LD  BC,&7fc4
OUT (C),C
LD  HL,&4000 
LD  DE,&C000 
LD  BC,&4000 
LD  IX,F_MOVE
CALL ROM_A2C 

LD  HL,TXT 
CALL TER_BB
call FRAME
ret

FRAME:
  LD B,&F5
FRA_L
  IN A,(C)
  RRCA     

  JR NC,FRA_L
RET 

LABIER
LD A,&03
LD (REG08_4),A ;Laden in E-RAM

LD HL,&4000
LD (REG16_3),HL ;Ziel-Adresse ist &4000 im E-RAM bei 16 KB Bildern

LD A,(MEDIUM) ;Quell-Medium

LD IX,LADE_N
CALL ROM_A2C ;Lade Bild

INC A
JR NZ,LB_ERR ;A<>&FF --> Fehler beim Laden!

LD BC,&FA7E
LD A,(MO_ST)
OUT (C),A ;Laufwerks-Motoren ausschalten

LD BC,&7FC0
OUT (C),C ;Bank in Main RAM

LD A,&FF
RET ;A = &FF --> Bild wurde erfolgreich geladen!

;Fehler beim Laden
LB_ERR
LD BC,&FA7E
LD A,(MO_ST)
OUT (C),A
LD BC,&7FC0
OUT (C),C
XOR A
RET

;Switch ROM char set on = lower RAM on
romchar:
LD  A,(RAMCHAR) ;actual RAM/ROM state
RES 2,A
LD  (RAMCHAR),A ;set L-RAM on
LD  B,&7F
OUT (C),A
ret

;Switch ROM char set off = lower ROM off
ramchar:
LD  A,(RAMCHAR) ;actual RAM/ROM state
SET 2,A
LD  (RAMCHAR),A ;set L-ROM off
LD  B,&7F
OUT (C),A
ret

H_ALLET EQU &C02F
TER_GB EQU &CDF6
TER_BB EQU &CD4C
TER_GG EQU &CE80
TER_RR EQU &CF2A
AKT_RAM EQU &B84C
F_MOVE EQU &C0C8
LADE_N EQU &FD5C
REG_PC  EQU &B8DA ;RAM
REG08_4 EQU &B8E0
REG16_3 EQU &B8EA
MO_ST  EQU &B97F
STR_BB EQU &CA1C
C_POS  EQU &B848
WATA  EQU &FD38
TUR_E EQU &FE9D
RAMCHAR EQU &B847
LESC EQU &C017
ROM_A2C EQU &FF2A
OSRON_A EQU &FF22
OSRON_B EQU &FF58
OSRON_C EQU &FF8E
OSRON_D EQU &FFD6
ROM_A  EQU &FF00
ROM_C  EQU &FF0C
ROM_D  EQU &FF12
ROM_D2A EQU &FFBA

S64X32 EQU &D5A8
S80X25 EQU &D60E

BORDER EQU &BB91
INK_0  EQU &BB92
INK_1  EQU &BB93
INK_2  EQU &BB94
INK_3  EQU &BB95

TERM_2  EQU &D48C
TERM_2D EQU &D2F7
TERM_2I EQU &D358
TERM_2K EQU &D3C0
TERM_2U EQU &D42

GUNHED

Please put it on disc, the picture to be loaded is missing. Also there is a missing letter in the source code: TERM_2U EQU &D42C
http://futureos.de --> Get the revolutionary FutureOS (Update: 2024.10.27)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

funkheld

#177
This Picture is in the BILDF001.ZIP 1,33kb

"C" is been stolen...

greeting.


Urusergi

#178
Unlocking the ASIC on the Amstrad Plus & GX4000 machines

This is the classic optimized routine (32 bytes) to unlock the ASIC with the sequence stored in memory

di ; 32 bytes
ld bc,#BC11
ld hl,seq

loop: inc b
outi
dec c
jr nz,loop

ei
ret

seq defb &FF,&00,&FF,&77,&B3,&51,&A8,&D4,&62,&39,&9C,&46,&2B,&15,&8A,&CD,&EE

But there is a cheaper way (30 bytes) to generate the same sequence 8)

di ; v3.0 -> 30 bytes!
ld bc,#BCFF
out (c),c
out (c),0 ; db #ED,#71
ld a,c

loop: out (c),a
ld h,a ; h = 7654 3210
add hl,hl ; h = 6543 210*
rra ; a = 7765 4321
add hl,hl ; h = 5432 10**
xor h:and #F7:xor h ; a = 7765 1321
add hl,hl ; h = 4321 0***
ld l,a ; l = 7765 1321
ld a,h ; a = 4321 0***
and #88:xor l ; a = (7 xor 4)765 (1 xor 0)321
cp c
jr nz,loop

ei
ret

andycadley

Interesting. I'm not sure if the obfuscation is worth it for two bytes in code you only ever use once, but might be handy in size coding challenges I guess.

Urusergi

Small speed update (-30us):

; Unlock CPC+ additional features

di ; v3.1 -> 30 bytes!
ld bc,#BCFF
out (c),c
out (c),0 ; db #ED,#71
ld a,c

loop: out (c),a
ld h,a ; h = 7654 3210
add hl,hl ; h = 6543 210*
rra ; a = 7765 4321
add hl,hl ; h = 5432 10**
xor h:and #F7:xor h ; a = 7765 1321
ld l,a ; l = 7765 1321
ld a,h ; a = 5432 10**
rla ; a = 4321 0***
and #88:xor l ; a = (7 xor 4)765 (1 xor 0)321
cp c
jr nz,loop

ei
ret


Quote from: andycadley on 17:19, 06 April 25but might be handy in size coding challenges I guess.

I agree, but this little routine could also be included in the future best game of all time for this platform,... or not? @norecess464 :-*

norecess464

As mentioned by @andycadley , I prefer the classic version, which is easily debuggable if a problem occurs. 

That said, your routine still has merit, especially if we see one day a very small production (a 512 bytes demo/game/etc) unlocking the ASIC.
My personal website: https://norecess.cpcscene.net
My current project is Sonic GX, a remake of Sonic the Hedgehog for the awesome Amstrad GX-4000 game console!

Urusergi

Quote from: norecess464 on 21:00, 07 April 25As mentioned by @andycadley , I prefer the classic version, which is easily debuggable if a problem occurs.

I knew it ;D

But this routine is bombproof. It's based on the electrical diagram of the Amstrad patent 'Controlling access to computer system features' and will always generate the correct sequence.

Obviously you are the owner of the creature, you can do whatever you want, so no problem ;)



I don't think it can be improved more, so I'll leave it at v3.1

andycadley

Quote from: Urusergi on 21:33, 07 April 25
Quote from: norecess464 on 21:00, 07 April 25As mentioned by @andycadley , I prefer the classic version, which is easily debuggable if a problem occurs.

I knew it ;D

But this routine is bombproof. It's based on the electrical diagram of the Amstrad patent 'Controlling access to computer system features' and will always generate the correct sequence.

Obviously you are the owner of the creature, you can do whatever you want, so no problem ;)



I don't think it can be improved more, so I'll leave it at v3.1

Oh, I don't doubt that and I will definitely keep a copy of this in my arsenal, just in case. I just can't think of many situations where saving two bytes beats the readability of the "classic" solution. I can verify in a couple of seconds that it is right (at most having to compare the byte sequence for the unlock) whereas with yours I'd have to put a lot more mental effort in if I thought I had a bug occurring there.

Prodatron

Quote from: Urusergi on 14:54, 06 April 25Unlocking the ASIC on the Amstrad Plus & GX4000 machines
But there is a cheaper way (30 bytes) to generate the same sequence 8)
Very cool, @Urusergi , thanks a lot, I would use your version in the future, why not saving some bytes :D

GRAPHICAL Z80 MULTITASKING OPERATING SYSTEM

norecess464

And now waiting for the LOCKING sequence...  8)
My personal website: https://norecess.cpcscene.net
My current project is Sonic GX, a remake of Sonic the Hedgehog for the awesome Amstrad GX-4000 game console!

Urusergi

Quote from: norecess464 on 21:00, 07 April 25That said, your routine still has merit

Merit? I don't think so, it should have existed since 1992.

Quote from: Prodatron on 10:05, 08 April 25Very cool, @Urusergi , thanks a lot, I would use your version in the future, why not saving some bytes :D

Thank you very much, I appreciate it, especially coming from a big one :)

Quote from: norecess464 on 14:25, 08 April 25And now waiting for the LOCKING sequence...  8)

Another great one has already taken care about that. It's thanks to him that I started to investigate this algorithm.

I'm talking about Yves Gerey, better known by his alias, Madram.

His idea can be seen in CPCWIKI

Obviously you won't like it because it takes up more than 32 bytes and looks like my code, but that's the least of it, the important thing to me is the idea of the algorithm.

It has been a nice challenge to get the counter down to 30 bytes :D


Me despido de todos, gracias por los likes :-*

McArti0

You can make a newer version or even an older version 1 byte shorter.
Instead of DI we do HALT and delete EI.  8)
CPC 6128, Whole 6128 and Only 6128, with .....
NewPAL v3 for use all 128kB RAM by CRTC as VRAM
One chip driver for 512kB(to640) extRAM 6128
TYPICAL :) TV Funai 22FL532/10 with VGA-RGB-in.

McArti0

Quote from: norecess464 on 14:25, 08 April 25And now waiting for the LOCKING sequence...  8)
Simple...
10 PRINT "Press any hardware reset"
20 GOTO 20
CPC 6128, Whole 6128 and Only 6128, with .....
NewPAL v3 for use all 128kB RAM by CRTC as VRAM
One chip driver for 512kB(to640) extRAM 6128
TYPICAL :) TV Funai 22FL532/10 with VGA-RGB-in.

andycadley

Quote from: McArti0 on 06:16, 09 April 25You can make a newer version or even an older version 1 byte shorter.
Instead of DI we do HALT and delete EI.  8)
Doesn't necessarily work, the Plus can generate interrupts much closer together than the old school CPC.

Although 90% of the time you're going to be doing this once in the startup sequence of the lower ROM and Z80 interrupts always start disabled anyway.

McArti0

Quote from: andycadley on 08:22, 09 April 25Doesn't necessarily work, the Plus can generate interrupts much closer together than the old school CPC.
Realy? Before unLock? 8)

Of course old CPC can generate interrupts much closer together. We can add few R0 Lo value lines.
CPC 6128, Whole 6128 and Only 6128, with .....
NewPAL v3 for use all 128kB RAM by CRTC as VRAM
One chip driver for 512kB(to640) extRAM 6128
TYPICAL :) TV Funai 22FL532/10 with VGA-RGB-in.

andycadley

Quote from: McArti0 on 08:36, 09 April 25
Quote from: andycadley on 08:22, 09 April 25Doesn't necessarily work, the Plus can generate interrupts much closer together than the old school CPC.
Realy? Before unLock? 8)

Of course old CPC can generate interrupts much closer together. We can add few R0 Lo value lines.
Just because the ASIC is locked, it doesn't mean it hasn't previously been unlocked and locking the ASIC doesn't disable any functionality, it just prevents you changing it.

Set up all three DMA channels to just loop and interrupt and you can get multiple INTs per scanline.

Urusergi

Quote from: norecess464 on 14:25, 08 April 25And now waiting for the LOCKING sequence...  8)

I was wrong. :doh: Curiosly, Madram's routine is the most optimal way to lock the ASIC:

;locking CPC+ additional features
di ; 30 bytes!
ld bc,#bcff
out (c),c
out (c),0 ; db #ED,#71
ld hl,#90ea

loop: out (c),c
ld a,h:rlca:ld h,l:ld l,a
srl c
xor c:and #88:xor c
ld c,a
cp #93
jr nz,loop

ei
ret

This routine generates the following sequence:

#ff,#00,#ff,#77,#b3,#51,#a8,#d4,#62,#39,#9c,#46,#2b,#15,#8a,#4d,#26



Quote from: McArti0 on 07:50, 09 April 25Simple...
10 PRINT "Press any hardware reset"
20 GOTO 20

:laugh: lmfao!

Powered by SMFPacks Menu Editor Mod