Z80 Programming VII - Part 3 |
ROM Monitor: Function C - CLEAR screen |
As we mentioned on the previous page, we initialize the UART in line 45, display the ROM Monitor header information in line 46, then jump to MON_PROMPT_LOOP which is the monitor prompt loop where we wait for you to type any 1 of 11 letters that map to monitor commands. We covered the UART initialization back in the UART Circuits module, RS232 Tests module, and the UART Programming module. I'm hoping that was enough. If not, we'll you have links to all three for quick reviews.
The jump to MON_PROMPT_LOOP displays the top 4 lines of the terminal emulator output in the adjacent screenshot. Pressing "?" as per the fourth line provides the "ZMC80 Monitor Command List". In this revision there are 11 commands. In the assembly code below the monitor screenshot you can see the CLEAR_SCREEN function that's invoked with the first command, "C - CLEAR screen". Let's have a look at it.
There are ANSI escape sequences we could use to clear the screen and home the cursor ("ESC[2J" and "ESC[f"), or we could use use the ASCII "\f" formfeed command ($0C). Instead we're going to send a bunch of newline commands. ANSI commands like "\r\n" will send ASCII CR and LF symbols. As you can see in the ASCII chart, numerically they are $0D for CR and $0A for LF. You probably know them as Carriage Return and Line Feed. (Make sure you click on the Hexadecimal link at the top of the ASCII chart screen if you are not seeing the hex values.)
Let's have a look at the CLEAR_SCREEN function that you can call using the letter "C" within the ROM Monitor. The code is to the right.
Register BC is used as a counter in line 59 and is preloaded with 24 for 24 newlines we'll invoke. In line 61, we save the current value of BC in case the PRINT_NEW_LINE function that follows also uses BC and may change it. A smarter choice would be to fully inspect that function to see if we really need to protect BC. After the newline function is called, we restore BC. In line 64, the "DJNZ CLS" instruction (Decrement B and Jump if Non-Zero) does just that - it decrements the BC register and then jumps to label "CLS" if BC is not equal to zero. Now you know why we chose BC for this job. BC is only at 23 after the decrement so the Program Counter jumps to line 60 and repeats the function. After 24 jumps register BC will finally be at zero so the program will fall through to the "RET" and return to the program calling the CLEAR_SCREEN function. That's all there is to this call - short and sweet. |