Launch rocket with countdown and acceleration in stdout: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Removed unnecessary import.) |
Not a robot (talk | contribs) (Add 8080 assembly) |
||
Line 4: | Line 4: | ||
The task is to simulate the countdown of a rocket launch from 5 down to 0 seconds and then display the moving, accelerating rocket on the standard output device as a simple ASCII art animation. |
The task is to simulate the countdown of a rocket launch from 5 down to 0 seconds and then display the moving, accelerating rocket on the standard output device as a simple ASCII art animation. |
||
=={{header|8080 Assembly}}== |
|||
This program runs under CP/M. It assumes a terminal that understands the |
|||
ASCII CR, LF, and FF control codes (in practice this will be all of them). |
|||
If your machine has blinkenlights (e.g. an Altair 8800), the top half of the address bus |
|||
will additionally show a countdown going from 5 to 0 lights. |
|||
The 8080 is assumed to be clocked at 2 Mhz, which was the rated maximum |
|||
for the earlier chips. If you overclock your Altair, or run this program on a later Z80-based machine, |
|||
the countdown will go faster. In a non-cycle-accurate emulator, the program will run in an instant. |
|||
In SIMH, the command <code>d clock 2000</code> might help. |
|||
<lang 8080asm> org 100h |
|||
lxi d,rocket ; Clear screen and print rocket |
|||
mvi c,9 |
|||
call 5 |
|||
mvi a,0F8h ; Five lights |
|||
lxi h,number |
|||
cdwn: lxi d,count ; Print countdown |
|||
call sout |
|||
call sec ; Wait a second |
|||
dcr m ; Decrease number |
|||
add a ; Turn off a light |
|||
jnz cdwn ; Again (if not zero yet) |
|||
lxi d,flame ; Print flame |
|||
call sout |
|||
mvi c,24 ; Lines |
|||
lxi h,20000 ; Liftoff timer |
|||
lift: lxi d,newln |
|||
call sout |
|||
call waithl |
|||
lxi d,-800 |
|||
dad d |
|||
dcr c |
|||
jnz lift |
|||
ret |
|||
sout: push psw ; Print string [DE] preserving registers |
|||
push b |
|||
push h |
|||
mvi c,9 |
|||
call 5 |
|||
jmp rsto |
|||
;;; Busy wait counting down HL. |
|||
;;; 52 + 24*HL cycles = 26 + 12*HL nanoseconds |
|||
waithl: push psw ; 11 cycles |
|||
push h ; 11 cycles |
|||
spin: dcx h ; 5 cycles ---- |
|||
mov a,h ; 5 cycles |
|||
ora l ; 4 cycles loop |
|||
jnz spin ; 10 cycles ---- |
|||
pop h ; 10 cycles |
|||
pop psw ; 10 cycles |
|||
ret ; 10 cycles |
|||
;;; Busy wait approximately one second (assuming 2Mhz clock) |
|||
;;; Pattern in A on address bus (will appear on blinkenlights |
|||
;;; if there are any). |
|||
sec: push psw |
|||
push b |
|||
push h |
|||
mov b,a ; Set up for pattern |
|||
lxi h,25000 ; 25.000 * 80 = 2.000.000 cycles ~= 1 sec |
|||
swait: ldax b ; 56 cycles, and hold pattern on blinkenlights |
|||
ldax b ; ... |
|||
ldax b |
|||
ldax b |
|||
ldax b |
|||
ldax b |
|||
ldax b |
|||
ldax b |
|||
dcx h ; 5 cycles |
|||
mov a,h ; 5 cycles |
|||
ora l ; 4 cycles |
|||
jnz swait ; 10 cycles |
|||
rsto: pop h |
|||
pop b |
|||
pop psw |
|||
ret |
|||
;;; Rocket |
|||
rocket: db 12, 10,10,10,10,10, 10,10,10,10,10 |
|||
db ' |',13,10 |
|||
db ' / ',92,13,10 |
|||
db ' / _ ',92,13,10 |
|||
db ' |.o ',39,'.|',13,10 |
|||
db ' |',39,'._.',39,'|',13,10 |
|||
db ' | |',13,10 |
|||
db ' ,',39,'| | |`.',13,10 |
|||
db ' / | | | ',92,13,10 |
|||
db ' |,-',39,'--|--',39,'-.|' ,13,10,'$' |
|||
flame: db ' ******* ',13,10 |
|||
db ' .*****.',13,10 |
|||
db ' .***.',13,10 |
|||
db ' .*.',13,10 |
|||
db ' .',13,10,'$' |
|||
count: db ' _____ ' |
|||
number: db '5 _____',13,'$' |
|||
newln: db 13,10,'$'</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |