ASxxxx Assembler V02.00 + NoICE + SDCC mods  (Zilog Z80 / Hitachi HD64180), page 1.
Hexadecimal [16-Bits]



                              1 ;;-----------------------------LICENSE NOTICE------------------------------------
                              2 ;;  This file is part of CPCtelera: An Amstrad CPC Game Engine 
                              3 ;;  Copyright (C) 2018 ronaldo / Fremos / Cheesetea / ByteRealms (@FranGallegoBR)
                              4 ;;
                              5 ;;  This program is free software: you can redistribute it and/or modify
                              6 ;;  it under the terms of the GNU Lesser General Public License as published by
                              7 ;;  the Free Software Foundation, either version 3 of the License, or
                              8 ;;  (at your option) any later version.
                              9 ;;
                             10 ;;  This program is distributed in the hope that it will be useful,
                             11 ;;  but WITHOUT ANY WARRANTY; without even the implied warranty of
                             12 ;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                             13 ;;  GNU Lesser General Public License for more details.
                             14 ;;
                             15 ;;  You should have received a copy of the GNU Lesser General Public License
                             16 ;;  along with this program.  If not, see <http://www.gnu.org/licenses/>.
                             17 ;;-------------------------------------------------------------------------------
                             18 .module cpct_strings
                             19 
                             20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                             21 ;;
                             22 ;; Function: cpct_setDrawCharM0
                             23 ;;
                             24 ;;    Sets foreground and background colours that will be used by <cpct_drawCharM0_inner>
                             25 ;; when called. <cpct_drawCharM0_inner> is used by both <cpct_drawCharM0> and <cpct_drawStringM0>.
                             26 ;;
                             27 ;; C Definition:
                             28 ;;    void <cpct_setDrawCharM0> (<u8> *fg_pen*, <u8> *bg_pen*) __z88dk_callee
                             29 ;;
                             30 ;; Input Parameters (2 Bytes):
                             31 ;;  (1B L )  fg_pen       - Foreground palette colour index (Similar to BASIC's PEN, 0-15)
                             32 ;;  (1B H )  bg_pen       - Background palette colour index (PEN, 0-15)
                             33 ;;
                             34 ;; Assembly call (Input parameters on registers):
                             35 ;;    > call cpct_setDrawCharM0_asm
                             36 ;;
                             37 ;; Parameter Restrictions:
                             38 ;;  * *fg_pen* must be in the range [0-15]. It is used to access a colour mask table and,
                             39 ;; so, a value greater than 15 will return a random colour mask giving unpredictable 
                             40 ;; results (typically bad character rendering, with odd colour bars).
                             41 ;;  * *bg_pen* must be in the range [0-15], with identical reasons to *fg_pen*.
                             42 ;;
                             43 ;; Requirements and limitations:
                             44 ;;  * This function *will not work from ROM*, as it uses self-modifying code.
                             45 ;;
                             46 ;; Details:
                             47 ;;    This function sets the internal values of <cpct_drawCharM0_inner>, so that next calls
                             48 ;; to <cpct_drawCharM0> or <cpct_drawStringM0> will use these new values. Concretely, these
                             49 ;; values are *fg_pen* (foreground colour) and *bg_pen* (background colour). This function
                             50 ;; receives 2 colour values in the range [0-15] and transforms them into the 4 possible 
                             51 ;; combinations of these 2 colours in groups of 2 pixels. Namely, 
                             52 ;; (start code)
                             53 ;;                       ___________________
                             54 ;;  2-bit combinations  | 00 | 01 | 10 | 11 |   * b = pixel coloured using bg_pen (background)
                             55 ;;  Displayed pixels    | bb | bF | Fb | FF |   * F = pixel coloured using fg_pen (foreground)
ASxxxx Assembler V02.00 + NoICE + SDCC mods  (Zilog Z80 / Hitachi HD64180), page 2.
Hexadecimal [16-Bits]



                             56 ;;                      \-------------------/
                             57 ;; (end code)
                             58 ;;    In mode 0, each of these 4 possible combinations corresponds to 1 byte in screen pixel 
                             59 ;; format that encodes the 2 selected colours.
                             60 ;;
                             61 ;;    These 4 combinations are stored into an internal array called *dc_2pxtableM0*, which is
                             62 ;; used by <cpct_drawCharM0_inner> to draw characters on the screen in mode 0, 2-pixels at a time.
                             63 ;; This array remains constant unless a new call to <cpct_setDrawCharM0> changes it. This lets
                             64 ;; the user change colours once and use them many times for subsequent calls to draw functions.
                             65 ;;
                             66 ;;    The appropriate use of this function is to call it each time a new pair of colours is
                             67 ;; required for following character drawings to be made either with <cpct_drawCharM0> or with
                             68 ;; <cpct_drawStringM0>. If the same colours are to be used for many drawings, a single call to 
                             69 ;; <cpct_setDrawCharM0> followed by many drawing calls would be the best practice. 
                             70 ;;
                             71 ;;    You may found use examples consulting documentation for <cpct_drawCharM0> and <cpct_drawStringM0>.
                             72 ;;
                             73 ;; Destroyed Register values: 
                             74 ;;    AF, BC, DE, HL
                             75 ;;
                             76 ;; Required memory:
                             77 ;;    ASM bindings  - 37 bytes (+100 cpct_drawCharM0_inner = 137 bytes)
                             78 ;;      C bindings  - 35 bytes (+100 cpct_drawCharM0_inner = 135 bytes)
                             79 ;;
                             80 ;; Time Measures:
                             81 ;; (start code)
                             82 ;;   Case     | microSecs (us) | CPU Cycles
                             83 ;; -------------------------------------------
                             84 ;;   Any      |      59        |     236
                             85 ;; -------------------------------------------
                             86 ;; Asm saving |      -9        |     -36
                             87 ;; -------------------------------------------
                             88 ;; (end code)
                             89 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                             90 
                             91 ;; Global symbols
                             92 ;;
                             93 .globl dc_mode0_ct   ;; Colour conversion table (16 values to convert PEN values [0-15] to their screen pixel format)
                             94 .globl dc_2pxtableM0 ;; Screen Pixel Format conversion table (4 pairs of pixels BG-BG, BG-FG, FG-BG, FG-FG)
                             95 
                             96 .module cpct_strings
                             97 
                             98 ;;
                             99 ;; Include constants and general values
                            100 ;;
ASxxxx Assembler V02.00 + NoICE + SDCC mods  (Zilog Z80 / Hitachi HD64180), page 3.
Hexadecimal [16-Bits]



                            101 .include "strings/strings.s"
                              1 ;;-----------------------------LICENSE NOTICE------------------------------------
                              2 ;;  This file is part of CPCtelera: An Amstrad CPC Game Engine 
                              3 ;;  Copyright (C) 2014-2015 ronaldo / Fremos / Cheesetea / ByteRealms (@FranGallegoBR)
                              4 ;;
                              5 ;;  This program is free software: you can redistribute it and/or modify
                              6 ;;  it under the terms of the GNU Lesser General Public License as published by
                              7 ;;  the Free Software Foundation, either version 3 of the License, or
                              8 ;;  (at your option) any later version.
                              9 ;;
                             10 ;;  This program is distributed in the hope that it will be useful,
                             11 ;;  but WITHOUT ANY WARRANTY; without even the implied warranty of
                             12 ;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                             13 ;;  GNU Lesser General Public License for more details.
                             14 ;;
                             15 ;;  You should have received a copy of the GNU Lesser General Public License
                             16 ;;  along with this program.  If not, see <http://www.gnu.org/licenses/>.
                             17 ;;-------------------------------------------------------------------------------
                             18 ;#####################################################################
                             19 ;### MODULE: Strings                                               ###
                             20 ;#####################################################################
                             21 ;### Routines to print and manage characters and strings           ###
                             22 ;#####################################################################
                             23 ;
                             24 .module cpct_strings
                             25 
                             26 ;;
                             27 ;; Compilation control directives
                             28 ;;
                             29 ;; If true, interrupts can be disabled to get function parameters faster
                     0000    30 .equ let_disable_interrupts_for_function_parameters, 0  
                             31 
                             32 ;;
                             33 ;; Constant values
                             34 ;;
                     3800    35 .equ char0_ROM_address, 0x3800   ;; Address where definition of character 0 starts in ROM
                     007F    36 .equ GA_port_byte,      0x7F     ;; 8-bit Port of the Gate Array
                             37 
                             38 ;;
                             39 ;; External values
                             40 ;;
                             41 .globl _cpct_mode_rom_status       ;; defined in cpc_mode_rom_status.s
ASxxxx Assembler V02.00 + NoICE + SDCC mods  (Zilog Z80 / Hitachi HD64180), page 4.
Hexadecimal [16-Bits]



                            102 
                            103 ;;
                            104 ;; ASM bindings for <cpct_setDrawCharM0>
                            105 ;;
                            106 ;;   0 us, 0 bytes
                            107 ;;
   6BB8                     108 _mySetDrawCharM0_asm::
                            109    ;; Get Colour values converted to Screen Pixel Format
                            110    ;;
   6BB8 11 70 8E      [10]  111    ld    de, #dc_mode0_ct  ;; [3]  DE = Pointer to the start of the Conversion Colour Table
                            112   
                            113    ;; Convert foreground colour and store it into B
   6BBB 44            [ 4]  114    ld     b, h             ;; [1] Save Background Pen into C for later use
   6BBC 26 00         [ 7]  115    ld     h, #0            ;; [2] | HL = L    (Index of the colour into the table [0-15])
   6BBE 19            [11]  116    add   hl, de            ;; [3] HL = DE + L ([Table base + Index], Points to the converted value)
   6BBF 4E            [ 7]  117    ld     c, (hl)          ;; [2] C = Conversion of foreground colour to screen pixel format
                            118   
                            119    ;; Convert background colour and store it into C
   6BC0 26 00         [ 7]  120    ld     h, #0            ;; [2] |
   6BC2 68            [ 4]  121    ld     l, b             ;; [1] | HL = B    (Index of the colour into the table [0-15])
   6BC3 19            [11]  122    add   hl, de            ;; [3] HL = DE + B ([Table base + Index], Points to the converted value)
   6BC4 46            [ 7]  123    ld     b, (hl)          ;; [2] B = Conversion of background colour to screen pixel format
                            124 
                            125    ;; Produce the 4 combinations for pairs of pixels (BG-BG, BG-FG, FG-BG, FG-FG)
                            126    ;; and store them in the placeholders inside <cpct_drawCharM0>
   6BC5 21 3A 85      [10]  127    ld    hl, #dc_2pxtableM0;; [3] HL Points to the 4-byte colour conversion table inside cpct_drawCharM0
                            128 
                            129    ;; Calculate and store BG-BG mode 0 pixel pair
   6BC8 78            [ 4]  130    ld     a, b             ;; [1] A = Right Pixel is BG coloured
   6BC9 07            [ 4]  131    rlca                    ;; [1] Shift left so A = Left pixel is BG coloured
   6BCA B0            [ 4]  132    or     b                ;; [1] A = Left and Right pixels are BG coloured
   6BCB 77            [ 7]  133    ld    (hl), a           ;; [2] Store BG-BG pair of mode 0 pixels in the table
                            134    
                            135    ;; Calculate and store BG-FG mode 0 pixel pair
   6BCC 23            [ 6]  136    inc   hl                ;; [2] Point to next pair (BG-FG) in the table
   6BCD A8            [ 4]  137    xor    b                ;; [1] Set Right Pixel to 0 (Its value is equal to B)
   6BCE B1            [ 4]  138    or     c                ;; [1] Left Pixel: BG, Right Pixel: FG
   6BCF 77            [ 7]  139    ld    (hl), a           ;; [2] Store BG-FG pair of mode 0 pixels in the table
                            140 
                            141    ;; Calculate and store FG-BG mode 0 pixel pair
   6BD0 23            [ 6]  142    inc   hl                ;; [2] Point to next pair (FG-BG) in the table
   6BD1 07            [ 4]  143    rlca                    ;; [1] Left Pixel: FG, Right Pixel: xx
   6BD2 E6 AA         [ 7]  144    and   #0xAA             ;; [2] Right Pixel: 00
   6BD4 B0            [ 4]  145    or    b                 ;; [1] Left Pixel: FG, Right Pixel: BG
   6BD5 77            [ 7]  146    ld    (hl), a           ;; [2] Store FG-BG pair of mode 0 pixels in the table
                            147 
                            148    ;; Calculate and store FG-FG mode 0 pixel pair
   6BD6 23            [ 6]  149    inc   hl                ;; [2] Point to next pair (FG-BG) in the table
   6BD7 A8            [ 4]  150    xor    b                ;; [1] Left Pixel: FG, Right Pixel: 0
   6BD8 B1            [ 4]  151    or     c                ;; [1] Left Pixel: FG, Right Pixel: FG
   6BD9 77            [ 7]  152    ld    (hl), a           ;; [2] Store FG-FG pair of mode 0 pixels in the table
                            153 
                            154    ;; Everything set up. Now return
   6BDA C9            [10]  155    ret                     ;; [3] Return
