I don't really know the hardware,because is a big box ,and we never used them because they are broke.
Microprofessor MPF1-B,display consists of six 7-segment display(plus decimal point ) .has a total of 8 leds.
; guess.asm
;
; Number guessing game
;
; Author : San Bergmans
; Source : www.sbprojects.com
; Date : 25-1-2006
;
;------------------------------------------------------------------------
;
; Human and computer will take turns to guess each other's secret number
; in the range from 0 to 1023 (inclusive).
;
;------------------------------------------------------------------------
.CR Z80 Yeah! We're programming the Z80!
.OR 01800H RAM starts here, so are we
.TF guess.hex,HEX Create a plain hex dump file
;------------------------------------------------------------------------
; Constants
;------------------------------------------------------------------------
PLUS .EQ 010H + key Guess higher
MINUS .EQ 011H - key Guess lower
GO .EQ 012H GO key Accept input
DATA .EQ 014H DATA key Give up, MPF next
DEL .EQ 017H DEL key Clear input
PC .EQ 018H PC key Cheat
REG .EQ 01BH REG key Restart program
SA .EQ 00001000B Segment A
SB .EQ 00010000B Segment B
SC .EQ 00100000B Segment C
SD .EQ 10000000B Segment D
SE .EQ 00000001B Segment E
SF .EQ 00000100B Segment F
SG .EQ 00000010B Segment G
DP .EQ 01000000B Decimal point
;------------------------------------------------------------------------
; Monitor routines
;------------------------------------------------------------------------
SCAN .EQ 005FEH Scan display until a key pressed
SCAN1 .EQ 00624H Scan display once
HEX7 .EQ 00689H Convert digit to 7-segement
HEX7SG .EQ 00678H Convert 2 hex digits to 7-segment
TONE .EQ 005E4H Generate tone (C=freq, HL=cycles)
TONE1K .EQ 005DEH Sound a 1kHz tone during (HL)
;------------------------------------------------------------------------
; GUESS
; Start of the program.
; Now it's your turn to guess the secret number.
; Enter your guess and press GO to see whether you should guess higher
; (indicated by an H) or lower (indicated by an L)
;------------------------------------------------------------------------
GUESS LD A,R Randomize seed a little
LD (SEED),A
AGAIN CALL RANDOM Calculate a random secret number
LD IX,.TEXT Show "GUESS" until a key
XOR A,A Clear input value
LD (INPUT),A
LD (INPUT+1),A
LD (DSP_BFFR),A Clear right most display
LD (COUNTER),A Clear try counter
.LOOP ; Main loop during human turn
CALL SCAN Show display until key pressed
CP A,10 Was it a digit key?
JR NC,.NODIGIT No!
CALL DEC2BIN Add digit to input value
JR .LOOP Show intermediate result!
.NODIGIT CP A,REG Was it the REG key?
JR Z,GUESS Yes! Restart the program
CP A,DEL Was it the DEL key?
JR NZ,.NOTDEL No! Don't clear value
XOR A,A Clear the input value
LD (INPUT),A
LD (INPUT+1),A
CALL SHOW_VAL Show this value
JR .LOOP And wait for input again!
.NOTDEL CP A,PC Was it the PC key?
JR NZ,.NOTPC Nope!
CALL PEEK Give us a peek at the secret!
JR .LOOP And continue where we left off!
.NOTPC CP A,GO Was it the GO key?
JR NZ,.NOTGO Nope!
LD HL,COUNTER Increment try counter
INC (HL)
JR NZ,.SKIP No overflow!
DEC (HL) Stop at ridiculous 255 tries
.SKIP CALL COMPARE Compare input value to target
LD A,B Guessed the number?
AND A,A (test flags)
JR NZ,.LOOP Nope!
JR CORRECT Yes!
.NOTGO CP A,DATA DATA key?
JR NZ,.LOOP Nope! Ignore the key then
JP COMPUTER We give up, let the MPF try
.TEXT .DA #0
.DA #SA+SC+SD+SF+SG S
.DA #SA+SC+SD+SF+SG S
.DA #SA+SD+SE+SF+SG E
.DA #SB+SC+SD+SE+SF U
.DA #SA+SC+SD+SE+SF G
;------------------------------------------------------------------------
; CORRECT
; We've guess the number correctly. Show YES and the number of tries now
;------------------------------------------------------------------------
CORRECT LD C,68 Sound short beep
LD HL,47 (same as default system beep)
CALL TONE Sound beep
LD A,(COUNTER) Convert number of tries to
CP A,100 decimal & 7-segment, limited
JR C,.SKIP to 99
LD A,99
.SKIP LD (INPUT),A
XOR A,A Counter is only 1 byte, so clear
LD (INPUT+1),A MSB
CALL SHOW_VAL
LD A,SD+SE+SF+SG Show "t" behind number of tries
LD (HL),A
LD HL,DSP_BFFR+5 Show "YES." in front of counter
LD A,SB+SC+SD+SF+SG
LD (HL),A
DEC HL
LD A,SA+SD+SE+SF+SG
LD (HL),A
DEC HL
LD A,SA+SC+SD+SF+SG+DP
LD (HL),A
CALL SCAN Show this until a key is pressed
JP COMPUTER Now it's the MPF's turn to guess
;------------------------------------------------------------------------
; DEC2BIN
; A new digit was entered. Multiply previous input value by 10, then add
; the new digit to form the updated input value.
;------------------------------------------------------------------------
DEC2BIN LD B,A Save new digit
LD A,(INPUT) Get previous value
LD L,A
LD A,(INPUT+1)
LD H,A
ADD HL,HL Multiply input value by 10
JR C,.OVERFLOW Number is too big!
LD D,H (x * 10 = x * 2 + x * 8)
LD E,L
ADD HL,HL
JR C,.OVERFLOW
ADD HL,HL
JR C,.OVERFLOW
ADD HL,DE
JR C,.OVERFLOW
LD A,B Add new input digit to it
ADD A,L
LD (INPUT),A
LD A,0
ADC A,H
LD (INPUT+1),A
JP NC,SHOW_VAL Digit added! Show current value
.OVERFLOW LD IX,.ERROR Show ERROR
XOR A,A Clear the input value
LD (INPUT),A
LD (INPUT+1),A
RET
.ERROR .DA #0
.DA #SE+SG r
.DA #SC+SD+SE+SG o
.DA #SE+SG r
.DA #SE+SG r
.DA #SA+SD+SE+SF+SG E
;------------------------------------------------------------------------
; SHOW_VAL
; Convert the value in INPUT to a 5 digit 7-segment number
; Conversion is done by repeatedly subtracting 10000 from the initial
; number for as long as it is possible. The number of successful
; subtractions is the most significant decimal digit. Then 1000 is
; repeatedly subtracted, etc, etc. Until finally a digit from 0 to 9
; remains, which is the least significant digit.
; At the same time we suppress leading zeroes, which makes it all look
; much nicer.
;------------------------------------------------------------------------
SHOW_VAL LD IX,DSP_BFFR+5 Use display buffer for result
LD IY,.DECADES Point to the decades table
LD A,(INPUT) Get value to be converted
LD L,A
LD A,(INPUT+1)
LD H,A
.LOOP LD A,(IY) Get current decade
LD E,A
INC IY
LD A,(IY)
LD D,A
INC IY IY now points to next decade
OR A,E Was end of table reached?
JR Z,.END Yes!
LD B,0 Clear decade value
.COUNT LD A,L Try to subtract decade from
SUB A,E value
LD C,A Temporarily save result
LD A,H
SBC A,D
JR C,.EXIT Didn't go!
LD H,A Save new intermediate result
LD L,C
INC B Increment decade counter
JR .COUNT Count as long as it goes!
.EXIT LD A,B Save this digit's value
LD (IX),A
DEC IX Point to next digit
JR .LOOP
.END LD A,L Last decade is in L now
LD (IX),A
LD IX,DSP_BFFR Reload display buffer pointer!
LD HL,DSP_BFFR+5 Convert the 5 decimal digits to
LD B,4 7-segment patterns (start left)
LD C,0 Leading 0's while this is 0
.7LOOP LD A,(HL) Get a decimal digit
OR A,C Leading 0?
JR Z,.SKIP Yes! Skip this one
LD C,A Set leading 0 flag now
LD A,(HL)
CALL HEX7 Convert digit to 7-segments
LD (HL),A and store it in display buffer
.SKIP DEC HL Do next digit (to the right)
DJNZ .7LOOP Do 4 of the 5 digits this way
LD A,(HL) Always display last digit,
CALL HEX7 even if the rest were all zeroes
LD (HL),A
DEC HL Blank last digit
LD (HL),0
RET
.DECADES .DA 10000
.DA 1000
.DA 100
.DA 10
.DA 0 Indicating end of conversion
;------------------------------------------------------------------------
; RANDOM
; Generate a pseudo random number between 0 and 1023
;------------------------------------------------------------------------
RANDOM LD A,(SEED) Get previous SEED value
LD L,A
LD A,(SEED+1)
LD H,A
LD D,L Calculate pseudo random number
LD E,6
SRL D
RR E
ADD HL,DE
LD A,L Save new SEED value
LD (SEED),A
LD A,H
LD (SEED+1),A
LD (HL),A
LD A,L Save secret number now
LD (SECRET),A
LD A,H
AND A,00000011B (limit number to 10 bits)
LD (SECRET+1),A
RET
;------------------------------------------------------------------------
; PEEK
; The user pressed the PC key to cheat! Show the secret number for as
; long the PC key is held down.
;------------------------------------------------------------------------
PEEK LD HL,INPUT Save current input value
LD E,(HL)
INC HL
LD D,(HL)
PUSH DE
LD A,(SECRET+1) Copy secret to input in order to
LD (HL),A be displayed
DEC HL
LD A,(SECRET)
LD (HL),A
CALL SHOW_VAL Convert this value to 7-segments
DEC HL Save right most display and then
LD A,(HL) clear it
PUSH AF
LD (HL),0
.LOOP CALL SCAN1 Display this value until the
JR NC,.LOOP key is released!
POP AF Restore right most display
LD (DSP_BFFR),A
POP DE Restore input value
LD HL,INPUT
LD (HL),E
INC HL
LD (HL),D
CALL SHOW_VAL Convert this value to 7-segments
RET
;------------------------------------------------------------------------
; COMPARE
; Compare the new input to the secret number. If the input is lower than
; the secret number an H is displayed behind the input value. If the
; input value is higher than the secret value an L is displayed. If
; the input value matches the secret number register B will be 0 so that
; main routine can take appropriate action.
;------------------------------------------------------------------------
COMPARE LD HL,INPUT Get input value
LD E,(HL)
INC HL
LD D,(HL)
LD HL,SECRET Compare it to the secret value
LD A,(HL)
SUB A,E
LD B,A
INC HL
LD A,(HL)
SBC A,D
JR C,.LOWER We're too high!
OR A,B See if we guessed correctly
LD B,A (Use B as flag upon return)
RET Z Got it!
.HIGHER LD A,SB+SC+SE+SF+SG Display an H
JR .DONE
.LOWER LD A,SD+SE+SF Display an L
.DONE LD (DSP_BFFR),A
XOR A,A Clear input value for next guess
LD (INPUT),A
LD (INPUT+1),A
RET
;------------------------------------------------------------------------
; COMPUTER
; Now it's the computer's turn to guess a secret number.
; The computer uses successive approximation to guess the secret number.
; Here's how it works:
;
; Initial guess 1000000000 Higher (leave last bit set)
; 2nd guess 1100000000 Lower (clear last bit)
; 3rd guess 1010000000 Higher (leave last bit set)
; 4th guess 1011000000 Lower (clear last bit)
; 5th guess 1010100000 Lower (clear last bit)
; 6th guess 1010010000 Higher (leave last bit set)
; 7th guess 1010011000 Higher (leave last bit set)
; 8th guess 1010011100 Lower (clear last bit)
; 9th guess 1010011010 Higher (leave last bit set)
; 10th guess 1010011011 This must be the secret number!
;
; You see that a mask bit shifts to the right after each guess. This bit
; remains set if we have to guess higher, or is cleared if we have to
; guess lower. Then it's shifted again, added to the previous result to
; form the new guess.
;------------------------------------------------------------------------
COMPUTER LD HL,MASK Set initial mask for successive
XOR A,A approximation
LD (HL),A
INC HL
LD (HL),00000010B
LD HL,INPUT Set initial accumulation value
LD (HL),A (which is in fact our first
INC HL guess)
LD (HL),00000010B
INC A Set guess counter to 1 because
LD (COUNTER),A we'll start right away
.LOOP CALL SHOW_VAL Convert value to 7-segments
LD (HL),SA+SB+SE+SG Display "?"
CALL SCAN Show this guess
CP A,PLUS Should we guess higher?
JR Z,.HIGHER Yes! Next guess should be higher
CP A,MINUS Should we guess lower?
JR Z,.LOWER Yes! Next guess must be lower
CP A,DATA DATA key?
JR Z,COMPUTER Yes! Restart computer's turn
CP A,REG REG key?
JP Z,GUESS Restart entire game (user's turn)
CP A,GO GO key?
JR Z,.RIGHT Yes! Guessed right, show tries
JR .LOOP Ignore all other keys!
.HIGHER CALL SHIFT_MASK Shift approximation mask right
CALL .ADDMASK Add new mask
JR .LOOP Show next try!
.LOWER LD HL,INPUT Undo last guess
LD A,(MASK)
XOR A,(HL)
LD (HL),A
INC HL
LD A,(MASK+1)
XOR A,(HL)
LD (HL),A
CALL SHIFT_MASK Shift approximation mask right
JR C,.END Run out of mask bits!
.OK CALL .ADDMASK Add new mask
JR .LOOP Take next guess!
.END LD HL,INPUT+1 Last bit was not supposed to
LD A,(HL) be cleared unless the guess
DEC HL is 0
OR A,(HL)
JR Z,.OK Was 0 indeed!
INC (HL) Set b0 of the guess again
JR .OK
.RIGHT LD A,(COUNTER) Show number of tries
LD (INPUT),A
XOR A,A
LD (INPUT+1),A
CALL SHOW_VAL Show this value
LD (HL),SD+SE+SF+SG Display "t"
CALL SCAN Show this until next key press
JP GUESS Then start all over again
.ADDMASK LD HL,INPUT Combine accumulation value with
LD A,(MASK) the new mask
LD B,A
OR A,(HL)
LD (HL),A
INC HL
LD A,(MASK+1)
LD C,A
OR A,(HL)
LD (HL),A
LD A,B Was MASK 0?
OR A,C
RET Z Yes! Don't increment counter!
LD HL,COUNTER Increment guess counter
INC (HL)
RET
;------------------------------------------------------------------------
; SHIFT_MASK
; Simple routine to right shift a 16-bit number
;------------------------------------------------------------------------
SHIFT_MASK LD HL,MASK+1 Shift mask 1 bit to the right
SRL (HL) starting with MSB of course
DEC HL
RR (HL)
RET
;------------------------------------------------------------------------
; RAM locations
; These locations are not included in the program code. This would allow
; the program to be burned in ROM. No code is generated here, only the
; labels get their appropriate values.
;------------------------------------------------------------------------
.OR 01D00H
.DU Don't generate any code
INPUT .BS 2 Input word value
SECRET .BS 2 Value to guess
SEED .BS 2 Random number seed
MASK .BS 2 Successive approximation mask
COUNTER .BS 1 Try counter
DSP_BFFR .BS 6 6 Bytes display buffer
.ED
;------------------------------------------------------------------------
.LI OFF
I know about that,it is the 7 segment display
RST $10 ; Afiseaza A pe ecran
The Z80 works a lot with reset vectors... Rst10 is software call that runs certain code.. Usually A is loaded with the call number... In this case Rst $10 is a print to display call and A contains the character...What does this instruction do?
Allen
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?