<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Espamática</title>
	<atom:link href="https://espamatica.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://espamatica.com</link>
	<description>Programación en 8 bits y algo más.</description>
	<lastBuildDate>Sun, 16 Feb 2025 20:46:03 +0000</lastBuildDate>
	<language>es</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>

<image>
	<url>https://i0.wp.com/espamatica.com/wp-content/uploads/2023/11/LogoWeb.png?fit=32%2C29&#038;ssl=1</url>
	<title>Espamática</title>
	<link>https://espamatica.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">147753459</site>	<item>
		<title>Uso de BeepFX</title>
		<link>https://espamatica.com/uso-de-beepfx/</link>
					<comments>https://espamatica.com/uso-de-beepfx/#comments</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Sat, 15 Feb 2025 19:47:17 +0000</pubDate>
				<category><![CDATA[Retro]]></category>
		<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Utilidades]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=6117</guid>

					<description><![CDATA[El objetivo de esta entrada es dar unas pequeñas nociones para ayudar a sacar el mayor provecho posible a BeepFX.]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">El objetivo de esta entrada es dar unas pequeñas nociones para ayudar a sacar el mayor provecho posible a <a href="https://shiru.untergrund.net/software.shtml" target="_blank" rel="noreferrer noopener nofollow">BeepFX</a>.</p>



<p class="has-medium-font-size wp-block-paragraph">Toda esto surge por la colaboración con <a href="https://x.com/juntelart" target="_blank" rel="noreferrer noopener nofollow">Juntelart</a> y su <a href="https://gm.retrojuegos.org/" target="_blank" rel="noreferrer noopener sponsored nofollow">ZX-Game Maker</a> para modificar la rutina de reproducción que genera BeepFX, y así evitar que los juegos que queden parados.</p>



<p class="has-medium-font-size wp-block-paragraph">Una vez terminada esta rutina, me encontré con la circunstancia de que diseñaba efectos un tanto aleatoriamente, pero sin tener muy claro lo que hacía. Como no sé si alguien más se encuentra en esta situación, he decidido escribir este artículo; espero que os sea de provecho.</p>



<p class="has-medium-font-size wp-block-paragraph">Antes de comenzar quiero agradecer los acertados comentarios que me han hecho mis compañeros del grupo Ensamblador Z80 de Telegram: <a href="https://conradobadenas.wordpress.com/" target="_blank" rel="noreferrer noopener nofollow">Conrado Badenas Mengod</a> y Pedro Picapiedra.</p>


<h2 class="simpletoc-title">Tabla de contenidos</h2>
<ul class="simpletoc-list">
<li><a href="#como-configurar-los-sonidos-con-beepfx">Cómo configurar los sonidos con BeepFX</a>

<ul>
<li><a href="#parametros-del-tono">Parámetros del tono</a>
</li>
<li><a href="#parametros-del-ruido">Parámetros del ruido</a>
</li>
<li><a href="#parametros-de-la-pausa">Parámetros de la pausa</a>
</li>
</ul>
</li>
<li><a href="#ejemplo-de-un-efecto-generado-con-beepfx">Ejemplo de un efecto generado con BeepFX</a>
</li>
<li><a href="#modificacion-del-reproductor-de-beepfx">Modificación del reproductor de BeepFX</a>
</li>
<li><a href="#como-usar-el-reproductor">Cómo usar el reproductor</a>
</li>
<li><a href="#conclusion">Conclusión</a>
</li></ul>


<h2 id="como-configurar-los-sonidos-con-beepfx" class="wp-block-heading has-large-font-size">Cómo configurar los sonidos con BeepFX</h2>



<p class="has-medium-font-size wp-block-paragraph">Cada sonido se configura de manera distinta, aunque semejante, por lo que os muestro por separado tanto sus parámetros como su uso.</p>



<h3 id="parametros-del-tono" class="wp-block-heading has-large-font-size">Parámetros del tono</h3>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="762" height="357" data-attachment-id="6121" data-permalink="https://espamatica.com/uso-de-beepfx/image-4/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image.png?fit=762%2C357&amp;ssl=1" data-orig-size="762,357" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="BeepFX, tono" data-image-description="" data-image-caption="&lt;p&gt;BeepFX, tono&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image.png?fit=762%2C357&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image.png?resize=762%2C357&#038;ssl=1" alt="BeepFX, tono" class="wp-image-6121" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image.png?w=762&amp;ssl=1 762w, https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image.png?resize=300%2C141&amp;ssl=1 300w" sizes="(max-width: 762px) 100vw, 762px" /><figcaption class="wp-element-caption">BeepFX, tono</figcaption></figure>



<ul class="wp-block-list">
<li class="has-medium-font-size"><strong>Frames</strong>: número de fracciones, no confundir con los típicos cincuenta frames por segundo (o sesenta) del ZX-Spectrum. Admite valores de 1 a 65536.</li>



<li class="has-medium-font-size"><strong>Frame length</strong>: longitud de cada uno de los frames. Admite valores de 1 a 65536. 1 segundo es aproximadamente 32768.</li>



<li class="has-medium-font-size"><strong>Pitch</strong>: frecuencia, este valor indica la nota. Admite valores de 1 a 65536.</li>



<li class="has-medium-font-size"><strong>Pitch slide</strong>: deslizamiento de la frecuencia. Se aplica a cada frame, por lo que no tiene efecto si sólo hay un frame; es el valor que se suma a la frecuencia en cada frame. Admite valores de -32767 a 32768.</li>



<li class="has-medium-font-size"><strong>Duty</strong>: ciclo de trabajo. En base a este valor se va a activar o desactivar el EAR y el altavoz interno. El valor medio es 128, valor con el que las notas suenan más claras. Cambiando este valor se distorsiona el sonido. Admite valores de 0 a 255. Con 0 no se escucha nada.</li>



<li class="has-medium-font-size"><strong>Duty slide</strong>: igual que Pitch Slide pero para Duty. Admite valores de -127 a 128.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">Ahora veamos como funcionan estos parámetros.</p>



<p class="has-medium-font-size wp-block-paragraph">Si configuras un bloque con 1 frame, 32768 de longitud, 440 de pitch, 0 de pitch slide, 128 de duty y 0 de duty slide, oirás el LA siguiente a DO central. Si ahora pones 10 frames con una longitud de 3276 y pitch slide de 100, verás como el efecto dura el mismo tiempo, pero se escuchan diez tonos distintos. El primer frame empieza en la frecuencia 440 y va sumando 100 a la frecuencia en los siguientes, resultando 540, 640, 740, etc.</p>



<p class="has-medium-font-size wp-block-paragraph">Las frecuencias del DO central y los once semitonos siguientes son:</p>



<figure class="wp-block-table is-style-stripes has-medium-font-size"><table class="has-fixed-layout"><thead><tr><th>Nota</th><th></th><th>Frecuencia</th></tr></thead><tbody><tr><td>DO</td><td>C</td><td>261,626</td></tr><tr><td>DO#</td><td>C#</td><td>277,183</td></tr><tr><td>RE</td><td>D</td><td>293,665</td></tr><tr><td>RE#</td><td>D#</td><td>311,127</td></tr><tr><td>MI</td><td>E</td><td>329,628</td></tr><tr><td>F</td><td>F</td><td>349,228</td></tr><tr><td>FA#</td><td>F#</td><td>369,994</td></tr><tr><td>SOL</td><td>G</td><td>391,995</td></tr><tr><td>SOL#</td><td>G#</td><td>415,305</td></tr><tr><td>LA</td><td>A</td><td>440,000</td></tr><tr><td>LA#</td><td>A#</td><td>466,164</td></tr><tr><td>SI</td><td>B</td><td>493,883</td></tr></tbody></table><figcaption class="wp-element-caption">BeepFX, frecuencias de las notas</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Aunque las frecuencias tienen decimales, tú siempre especificarás los valores enteros. Para subir una octava debes multiplicar el valor de la frecuencia por dos y para bajar una octava dividirlo entre dos, de ahí que se reflejen los valores decimales. Si subes tres octavas (2^3), al multiplicar por ocho el SI sin tener en cuenta los decimales da tres mil novecientos cuarenta y cuatro, pero teniendo en cuenta los decimales da tres mil novecientos cincuenta y uno sin tener en cuenta los decimales del resultado, una diferencia de siete, aunque no creo que se aprecie en demasía.</p>



<p class="has-medium-font-size wp-block-paragraph">Si te interesa saber más sobre esta frecuencias, Conrado me ha proporcionado este <a href="https://es.wikipedia.org/wiki/Frecuencias_de_afinaci%C3%B3n_del_piano" target="_blank" rel="noreferrer noopener nofollow">enlace</a>.</p>



<h3 id="parametros-del-ruido" class="wp-block-heading has-large-font-size">Parámetros del ruido</h3>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" decoding="async" width="762" height="357" data-attachment-id="6125" data-permalink="https://espamatica.com/uso-de-beepfx/image-5/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image-1.png?fit=762%2C357&amp;ssl=1" data-orig-size="762,357" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="image" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image-1.png?fit=762%2C357&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image-1.png?resize=762%2C357&#038;ssl=1" alt="BeepFX, ruído" class="wp-image-6125" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image-1.png?w=762&amp;ssl=1 762w, https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image-1.png?resize=300%2C141&amp;ssl=1 300w" sizes="(max-width: 762px) 100vw, 762px" /><figcaption class="wp-element-caption">BeepFX, ruido</figcaption></figure>



<ul class="wp-block-list">
<li class="has-medium-font-size"><strong>Frames</strong>: igual que Frames en los parámetros del tono.</li>



<li class="has-medium-font-size"><strong>Frame length</strong>: igual que Frame length en los parámetros del tono.</li>



<li class="has-medium-font-size"><strong>Pitch</strong>: parecido a Pitch en los parámetros del tono, pero admite valores de 1 a 256. Usa los valores que hay en los primeros 8Kb de la ROM para generar el ruido.</li>



<li class="has-medium-font-size"><strong>Pitch slide</strong>: igual que Pitch slide en los parámetros del tono.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">Fijaos que en Pitch indico que es semejante a Pitch en los parámetros del tono. Probad a configurar un ruido con Frames = 10, Frame length = 30000, Pitch = 128 y Pitch slide = 128. Al reproducir este bloque apreciaréis que hay una variación de sonido, digamos, entre los frames pares y los impares. Esto se debe a que en los ruidos, Pith y Pitch Slide son bytes, resultando que los valores de Pitch en cada frame son:</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>Frame</th><th class="has-text-align-right" data-align="right">Pitch</th></tr></thead><tbody><tr><td>1</td><td class="has-text-align-right" data-align="right">128</td></tr><tr><td>2</td><td class="has-text-align-right" data-align="right">0</td></tr><tr><td>3</td><td class="has-text-align-right" data-align="right">128</td></tr><tr><td>4</td><td class="has-text-align-right" data-align="right">0</td></tr></tbody></table><figcaption class="wp-element-caption">BeepFX, efecto de Pitch Slide sobre Pitch en los ruidos</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Y así hasta llegar al décimo frame.</p>



<p class="has-medium-font-size wp-block-paragraph">Para conseguir mayor variación de tonos pon un valor más bajo en Pitch Slide, por ejemplo treinta y dos.</p>



<h3 id="parametros-de-la-pausa" class="wp-block-heading has-large-font-size">Parámetros de la pausa</h3>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" decoding="async" width="762" height="357" data-attachment-id="6128" data-permalink="https://espamatica.com/uso-de-beepfx/image-6/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image-2.png?fit=762%2C357&amp;ssl=1" data-orig-size="762,357" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="image" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image-2.png?fit=762%2C357&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image-2.png?resize=762%2C357&#038;ssl=1" alt="BeepFX, pausa" class="wp-image-6128" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image-2.png?w=762&amp;ssl=1 762w, https://i0.wp.com/espamatica.com/wp-content/uploads/2025/02/image-2.png?resize=300%2C141&amp;ssl=1 300w" sizes="(max-width: 762px) 100vw, 762px" /><figcaption class="wp-element-caption">BeepFX, pausa</figcaption></figure>



<ul class="wp-block-list">
<li class="has-medium-font-size"><strong>Frames</strong>: igual que Frames en los parámetros del tono.</li>



<li class="has-medium-font-size"><strong>Frame length</strong>: igual que Frame length en los parámetros del tono.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">Si te fijas en el código ensamblador que genera BeepFX, la pausa es idéntica al tono (incluso se identifica como tal), pero con los parámetros Pitch, Pitch slide, Duty y Duty slide a cero.</p>



<p class="has-medium-font-size wp-block-paragraph">Esto es algo que tienes que tener muy en cuenta. Si tu sonido (o quizá música) va a reproducir un bloque en cada interrupción, pero para conseguir el compás correcto necesitas incluir pausas (notas que no suenan), estas pausas debes configurarlas con Frames = 1 y Frame length = 1. Durante esa interrupción, o en el bucle del programa, lo único que interesa es que la nota que suena no suene, así que cuánto menos dure mucho mejor.</p>



<h2 id="ejemplo-de-un-efecto-generado-con-beepfx" class="wp-block-heading has-large-font-size">Ejemplo de un efecto generado con BeepFX</h2>



<p class="has-medium-font-size wp-block-paragraph">A continuación, un ejemplo de un efecto generado con BeepFX que contiene los tres tipos de bloques especificados anteriormente (se muestra el código ensamblador que genera el programa).</p>



<pre class="wp-block-code has-medium-font-size"><code>SoundEffectsData:
  defw SoundEffect0Data

SoundEffect0Data:
  defb 1 ;tone
  defw 1,500,440,0,128
  defb 1 ;tone
  defw 1,500,440,0,128
  defb 1 ;pause
  defw 1,500,0,0,0
  defb 1 ;pause
  defw 1,500,0,0,0
  defb 2 ;noise
  defw 1,500,100
  defb 2 ;noise
  defw 1,500,100
  defb 0</code></pre>



<p class="has-medium-font-size wp-block-paragraph">En los bloques de tono y pausa, en las líneas que empiezan por <strong>defw</strong>, los valores son los siguientes (son valores de 16 bits):</p>



<figure class="wp-block-table has-medium-font-size"><table><thead><tr><th>Parámetro</th><th class="has-text-align-right" data-align="right">Valor</th><th>Observaciones</th></tr></thead><tbody><tr><td>Frames</td><td class="has-text-align-right" data-align="right">1</td><td></td></tr><tr><td>Frame lenght</td><td class="has-text-align-right" data-align="right">500</td><td></td></tr><tr><td>Pitch</td><td class="has-text-align-right" data-align="right">440</td><td></td></tr><tr><td>Pitch slide</td><td class="has-text-align-right" data-align="right">0</td><td></td></tr><tr><td>Duty<br>Duty slide</td><td class="has-text-align-right" data-align="right">128</td><td>Este valor resulta de Duty slide * 256 + Duty</td></tr></tbody></table><figcaption class="wp-element-caption">BeepFX, tono</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">En los bloques de ruido, en las líneas que empiezan por defw, los valores son los siguientes (vuelven a ser valores de 16 bits):</p>



<figure class="wp-block-table has-medium-font-size"><table><thead><tr><th>Parámetro</th><th class="has-text-align-right" data-align="right">Valor</th><th>Observaciones</th></tr></thead><tbody><tr><td>Frames</td><td class="has-text-align-right" data-align="right">1</td><td></td></tr><tr><td>Frame lenght</td><td class="has-text-align-right" data-align="right">500</td><td></td></tr><tr><td>Pitch<br>Pitch slide</td><td class="has-text-align-right" data-align="right">100</td><td>Este valor resulta de Pitch slide * 256 + Pitch</td></tr></tbody></table><figcaption class="wp-element-caption">BeepFX, ruido</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Los bloques de&nbsp;<strong>Tono</strong>&nbsp;y&nbsp;<strong>Pausa</strong>&nbsp;tienen una longitud de once bytes, desde el byte que indica el tipo de bloque, hasta el último byte del bloque. Los bloques de&nbsp;<strong>Ruido</strong>&nbsp;tienen una longitud de siete bytes desde el byte que indica el tipo de bloque hasta el último byte del bloque.</p>



<p class="has-medium-font-size wp-block-paragraph">En ensamblador, los valores separados por coma que se encuentran después de defb o db son de tipo byte (8 bits). Por el contrario, los valores que se encuentran después de defw o dw son de tipo word (palabra) o dos bytes (16 bits).</p>



<p class="has-medium-font-size wp-block-paragraph">Cada bloque está compuesto por una primera línea defb en la que se indica el tipo de bloque, y una segunda línea defw en la que se indican los parámetros del bloque.</p>



<h2 id="modificacion-del-reproductor-de-beepfx" class="wp-block-heading has-large-font-size">Modificación del reproductor de BeepFX</h2>



<p class="has-medium-font-size wp-block-paragraph">Con BeepFX puedes crear efectos realmente sorprendentes, incluso proporciona un reproductor para que no tengas que preocuparte de nada, pero aquí hay un gran problema: el reproductor reproduce todo el efecto, lo cual puede provocar, y provoca, que tus juegos se paren. En cuanto quieras tener un efecto elaborado, seguro que tu juego se detiene mientras se reproduce.</p>



<p class="has-medium-font-size wp-block-paragraph">Ya comenté al principio que este artículo surge a consecuencia de una conversación que tuve con Junterlart. He realizado una modificación sobre el reproductor de Shiru, para que se pueda reducir bloque a bloque en lugar de reproducir el efecto completo.</p>



<p class="has-medium-font-size wp-block-paragraph">Para comprender lo que hace la rutina de Shiru la he comentado línea a línea, lo cual me ha ayudado también a entender como funciona BeepFX. Esta es la rutina tal y como ha quedado.</p>



<pre class="wp-block-code has-medium-font-size"><code>; BeepFX player by Shiru
; You are free to do whatever you want with this code
; Modificado por Juan Antonio Rubio García

; Si se va a usar desde fuera de ASM, antes de Play poner ORG Dirección para que calcule las
; direcciones al ensamblar
; Cambios realizados para su uso en ZX-Game Maker

; Primero: en la dirección Play+$01 cargar el sonido a reproducir
;     LD   Play+$01,$03   POKE Play+1,3           Indica cargar el sonido 3
; Segundo: llamar a Play
;     CALL Play           RANDOMIZE USR Play      Pone en memoria la dirección del sonido 3
; Tercero: llamar a NextNote cuando sea preciso
;     CALL NextNote       RANDOMIZE USR Play+17   Reproduce un bloque del efecto y sale
;                                                 Si no hay ningún efecto cargado sale
Play:
  ld   c, $00           ; En $00 se indicará el sonido a reproducir
  ld   b, $00           ; BC = A, efecto a reproducir
  ld   hl,FXAddress     ; Dirección en la que están las direcciones de los efectos
  add  hl,bc
  add  hl,bc            ; HL = dirección donde está la dirección del sonido a reproducir
  ld   c, (hl)
  inc  hl
  ld   b, (hl)          ; BC = dirección del sonido a reproducir
  ld   (FX),bc          ; Guarda en memoria la dirección del sonido a reproducir
  ret

NextNote:
  ld   hl,(FX)          ; HL = dirección del siguiente bloque a reproducir
  ld   a, (hl)          ; Carga el tipo de efecto en A (es el primer byte del bloque)
  or   a                ; Comprueba si es 0
  ret  z                ; Si es 0 sale, nada que reproducir

  di                    ; Desactiva las interrupciones
  push ix
  push iy               ; Preserva los registros índice
  push hl
  pop  ix               ; Carga en IX el valor de HL (dirección del efecto/bloque), ver línea 29

  ld   a, ($5C48)       ; Obtiene los atributos del borde
  rra
  rra
  rra                   ; Los pone en los bits 0 a 2
  and  $07              ; Se queda con los atributos del borde
  ld (sfxRoutineToneBorder  +$01),a
  ld (sfxRoutineNoiseBorder +$01),a ; Modifica las rutinas con el valor del borde
                                    ; OR $00, sustituye 0 por el valor de A, en líneas 102 y 150

readData:
  ld   a, (ix+$00)      ; A = tipo de bloque
  ld   c, (ix+$01)
  ld   b, (ix+$02)      ; BC = Frames
  ld   e, (ix+$03)
  ld   d, (ix+$04)      ; DE = Frame lenght, estos tres parámetros son comunes en tono y ruido
  push de
  pop  iy               ; Carga en IY el valor de DE, Frame lenght

  dec  a                ; Decrementa A (Tipo de bloque)
  jr   z,sfxRoutineTone ; Si es 0, es tono, salta
  dec  a                ; Decrementa A
  jr   z,sfxRoutineNoise; Si es 0, es ruido, salta
endData:
  pop  iy
  pop  ix               ; Recupera los registros índice
  ei                    ; Reactiva las interrupciones
  ret                   ; Sale de la rutina
   
nextData:
  add  ix,bc            ; Apunta IX al bloque siguiente, ver líneas 129 y 183 para BC, líneas 37 y 38 para IX
  ld   (FX),ix          ; Guarda en memoria la dirección del siguiente bloque 
  jr   endData          ; Salta para salir de la rutina

; Genera tono con seis parámetros, 11 bytes por bloque. Frame length 32768 = 1 segundo aprox.
; Tono
;       0
;	      Type = Tone
;  defb 1
;       1 - 2 	3 - 4 	      5 - 6           7 - 8 	  9 - 10
;	Frames	Frame lenght  Pitch	      Pith slide  Duty y Duty slide
;                             Frecuencia
;  defw 65536,	65536,	      65535,	      32768,	  33023
sfxRoutineTone:
  ld   e, (ix+$05)      ; IX = dirección el bloque, ver líneas 37 y 38
  ld   d, (ix+$06)      ; DE = Pitch
  ld   a, (ix+$09)      ; A = Duty
  ld   (sfxRoutineToneDuty+1),a ; Modifica la rutina con el valor de Duty
                                ; CP $00, sustituye 0 por el valor de A, ver línea 98
  ld   hl,$00           ; HL = 0. Para reproducir el sonido va sumando Pitch (frecuencia)
                        ; y usa el byte alto para activar/desactivar EAR y altavoz interno
sfxRT0:
  push bc               ; Preserva BC (Frames), ver líneas 51 y 52
  push iy
  pop  bc               ; Carga en BC el valor de IY (Frame lenght), ver líneas 55 y 58
sfxRT1: ; Desde aquí la rutina tarda 88 t-sates hasta la línea 108 (sincronización).
  add  hl,de            ; Suma DE (Pitch) a HL (11 t-states)
  ld   a, h             ; Carga el byte alto en A (4 t-sates)
sfxRoutineToneDuty:
  cp   $00              ; Lo compara con el valor de Duty ($00 se modifica en las líneas 110 a 112, se carga la primera vez en línea 86) (7 t-sates) 
  sbc  a, a             ; Resta A - A teniendo en cuenta el acarreo (4 t-states)
  and  $10              ; Se queda con el bit 4, EAR y altavoz interno activado/desactivado (7 t-states)
sfxRoutineToneBorder:
  or   $00              ; Añade el color del borde (se modificó en las línea 45) (7 t-states)
  out  ($fe),a          ; Manda el valor al puerto 254 para activar/desactivar EAR y altavoz interno (11 t-sates)
  ld   a,(0)            ; (13 t-states)
  dec  bc               ; Decrementa BC (6 t-sates)
  ld   a, b             ; (4 t-states)
  or   c                ; Comprueba si BC = 0 (4 t-sates)
  jp  nz, sfxRT1        ; Si no es 0, bucle hasta que Frame lenght = 0 (10 t-states, Total 88 t-states)

  ld   a,(sfxRoutineToneDuty+1) ; Cambio de Duty en línea 98
  add  a,(ix+$0a)               ; Añade el valor de Duty slide
  ld   (sfxRoutineToneDuty+1),a ; Cambia el valor de la línea 98

  ld   c, (ix+$07)
  ld   b, (ix+$08)      ; BC = Pitch slide
  ex   de,hl            ; Intercambia los valores de DE y HL
  add  hl,bc            ; Suma Pitch slide a Pitch, Pitch se carga en líneas 83 y 84
  ex   de,hl            ; Intercambia los valores de DE y HL (DE = Pitch + Pitch slide)

  pop  bc               ; Recupera BC (Frames)
  dec  bc               ; Decrementa BC
  ld   a, b
  or   c                ; Comprueba si BC = 0
  jr   nz,sfxRT0        ; Si no es 0, bucle hasta que Frames = 0

  ld   c, $0b           ; C = bytes a los que está el siguiente bloque, BC viene a 0
  jp   nextData

; Genera ruido con dos parámetros, 7 bytes por bloque. Frame length 32768 = 1 segundo +/-.
;       0
;       Type = Noise
; defb  2 ;noise
;       1 - 2   3 - 4         5 - 6
;       Frames  Frame lenght  Pitch y Pitch slide
; defw  65535,  65535,        33023
sfxRoutineNoise:
  ld   e, (ix+$05)      ; E = Pitch, IX = dirección el bloque, ver líneas 37 y 38
  ld   d, $01
  ld   h, d
  ld   l, d             ; HL = $11 (Dirección 17 de la ROM)
sfxRN0:
  push bc               ; Preserva BC (Frames)
  push iy
  pop  bc               ; Carga en BC el valor de IY (Frame lenght), ver líneas 55 y 56
sfxRN1: 
; Desde aquí, la rutina tarda 112 t-states hasta la línea 168 si en la línea 153 D=0 y 88 si D&lt;>0
  ld   a, (hl)          ; A = Valor de la dirección a la que apunta HL (en los primeros 8Kb de la ROM) (7 t-states)
  and  $10              ; Se queda con el bit 4, activa o desactiva EAR y altavoz interno (7 t-states)
sfxRoutineNoiseBorder:
  or   $00              ; Añade el color del borde ($00 se modificó en la línea 46) (7 t-states)
  out  ($fe),a          ; Manda el valor al puerto 254 para activar/desactivar EAR y altavoz interno (11 t-sates)
  dec  d                ; Decrementa D (4 t-states)
  jp   z, sfxRN2        ; Si es 0 salta (10 t-states)
  nop                   ; (4 t-states)
  jp   sfxRN3           ; Salta (10 t-states)
sfxRN2:
  ld   d, e             ; D = Pitch (4 t-states)
  inc  hl               ; HL += 1 (6 t-states)
  ld   a, h             ; A = H (4 t-states)
  and  $1F              ; Se queda con el valor de los bits 0 a 4 (7 t-states)
  ld   h, a             ; H = A, dejando HL apuntando a alguna dirección de los primeros 8Kb de la ROM (4 t-states)
  ld   a, ($00)         ; (13 t-sates)
sfxRN3:
  nop                   ; (4 t-states)
  dec  bc               ; Decrementa BC (6 t-states)
  ld   a, b             ; (4 t-states)
  or   c                ; Comprueba si BC = 0 (4 t-states)
  jp   nz, sfxRN1       ; Si no es 0, bucle hasta que Frame lenght = 0 (10 t-states, Total 88 o 112 t-states)

  ld   a, e             ; A = Pitch
  add  a, (ix+$06)      ; A += Pitch slide
  ld   e, a             ; Actualiza Pitch

  pop  bc               ; Recupera BC (Frames)
  dec  bc               ; BC -= 1
  ld   a, b
  or   c                ; Comprueba si BC = 0
  jr   nz,sfxRN0        ; Si no es 0, bucle hasta que Frame = 0

  ld   c, $07           ; C = bytes a los que está el siguiente bloque, BC viene a 0
  jp   nextData

FX:
  dw   $001E            ; Se inicializa a $001E para que desde la primera vez salga de NextNote
                        ; si no tiene que reproducir nada (líneas 29 a 32)

FXAddress:
; Archivo asm generado por BeepFX sin reproductor
include "fx.asm"</code></pre>



<h2 id="como-usar-el-reproductor" class="wp-block-heading has-large-font-size">Cómo usar el reproductor</h2>



<p class="has-medium-font-size wp-block-paragraph">El reproductor tiene dos puntos de entrada, Play y NextNote. Lo primero que tienes que hacer es poner en la dirección de memoria Play+1 el número de efecto que quieres reproducir, y acto seguido llamar a Play. Posteriormente, cada vez que quieras reproducir un bloque, en cada interrupción, en cada iteración del bucle de tu juego, debes llamar a NextNote.</p>



<p class="has-medium-font-size wp-block-paragraph">En ensamblador sería algo así:</p>



<pre class="wp-block-code has-medium-font-size"><code>ld   (Play+$01), $01
call Play
...
...
...
call NextNote</code></pre>



<p class="has-medium-font-size wp-block-paragraph">En BASIC se haría de la siguiente manera, suponiendo que cargas el reproductor y los efectos a partir de la línea 50000:</p>



<pre class="wp-block-code has-medium-font-size"><code>POKE 50001,1
RANDOMIZE USR 50000:REM Llama a PLAY
...
...
...

RANDOMIZE USR 50017</code></pre>



<p class="has-light-green-cyan-background-color has-background has-medium-font-size wp-block-paragraph"><strong>No obstante, próximamente voy a publicar en <a href="https://playonretro.com/category/podcast-jarubio/" target="_blank" rel="noreferrer noopener nofollow sponsored">El taller de Juanan</a> en la web de <a href="https://playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Play On Retro</a>, una variación de la rutina que creo que simplifica su uso desde BASIC.</strong></p>



<p class="has-medium-font-size wp-block-paragraph">Ten en cuenta que los efectos que diseñes en BeepFX no van a sonar igual en tus juegos. La explicación a esto es que en tu juego vas a reproducir un bloque, por ejemplo, por cada interrupción, mientras que en BeepFX se reproduce todo el efecto completo. Para simular esto en BeepFX debes meter pausas ficticias de una interrupción; hay cincuenta interrupciones por segundo, por lo que una aproximación sería entre cada tono o ruido poner una pausa de un frame con una longitud de seiscientos cincuenta y cinco. Estas pausas ficticias se verán fácilmente identificadas como:</p>



<pre class="wp-block-code has-medium-font-size"><code>defb 1
defw 1,655,0,0,0</code></pre>



<p class="has-medium-font-size wp-block-paragraph">No olvides borrarlas del archivo generado por BeepFX.</p>



<p class="has-medium-font-size wp-block-paragraph">También es posible que tu efecto tenga pausas necesarias, es decir, no quieres que suene un bloque en cada interrupción, si no que quieres que haya más espacio. Para esto último incluye pausas de un frame y una longitud de uno, así las podrás distinguir fácilmente de las pausas para simular el modo de reproducción y no entorpecerán el flujo del juego.</p>



<p class="has-medium-font-size wp-block-paragraph">Una vez que tengas todos los efectos diseñados, en el menú&nbsp;<em>File</em>, haz clic sobre&nbsp;<em>Compile</em>&nbsp;y en la ventana emergente selecciona&nbsp;<em>Assembly</em>,&nbsp;<strong>quita el check a Include player code</strong>&nbsp;(no vamos a usar el reproductor de BeepFX) y haz clic sobre Compile (el resto de parámetros no son importantes en nuestro caso). En el diálogo Save compiled data, graba el archivo como fx.asm en el directorio dónde tengas los fuentes para que el ensamblador que uses pueda localizarlo en la línea include que hay al final del reproductor.</p>



<h2 id="conclusion" class="wp-block-heading has-large-font-size">Conclusión</h2>



<p class="has-medium-font-size wp-block-paragraph">Espero que todo lo aquí descrito os sirva para que vuestros juegos no se paren cuándo se reproduce algún efecto de sonido creado con BeepFX y en consecuencia fluyan mejor.</p>



<p class="has-medium-font-size wp-block-paragraph">La idea de esta modificación surgió cuándo Juntelart me comentó el problema al reproducir los efectos de sonido, que paraban los juegos, y creo que hemos dado con una solución decente.</p>



<p class="has-medium-font-size wp-block-paragraph"><strong>La colaboración hace la fuerza.</strong></p>



<p class="has-medium-font-size wp-block-paragraph">Puedes descargar el proyecto de pruebas, con el código fuente desde <a href="https://espamatica.com/category/zx-spectrum/">a</a><a href="https://espamatica.com/wp-content/uploads/2025/02/Prototipo.zip">q</a><a href="https://espamatica.com/category/zx-spectrum/">uí</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-medium-font-size wp-block-paragraph">También puedes visitar el resto de tutoriales:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">.Net
<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://espamatica.com/mediatime/">MediaTime</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/nullabledatetimepicker/">NullableDateTimePicker</a></li>
</ul>
</li>



<li class="has-medium-font-size">Z80
<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://espamatica.com/entorno-de-desarrollo-para-ensamblador-z80/">Entorno de desarrollo</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/numeros-bcd-spectrum/">Números BCD</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/posiciones-de-pantalla-zx-spectrum/">Pantalla ZX Spectrum</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/ensamblador-zx-spectrum-pong-introduccion/">Pong</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/0x00-ensamblador-zx-spectrum-marciano-introduccion/">Batalla Espacial</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/0x01-ensamblador-zx-spectrum-oxo-tablero-de-juego/">Tres en raya</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/uso-de-beepfx/">Uso de BeepFX</a></li>
</ul>
</li>
</ul>
</div>



<p class="has-medium-font-size wp-block-paragraph">Y recuerda, si lo usas no te limites a copiarlo, intenta entenderlo y adaptarlo a tus necesidades.</p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/uso-de-beepfx/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">6117</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly &#8211; Utilities</title>
		<link>https://espamatica.com/zx-spectrum-assembly-utilities/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-utilities/#comments</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Wed, 27 Mar 2024 16:49:44 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=5490</guid>

					<description><![CDATA[This is a compilation of different routines that can be useful to deal with certain aspects of the ZX Spectrum.]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">This is a compilation of different routines that can be useful to deal with certain aspects of the ZX Spectrum.</p>



<p class="has-medium-font-size wp-block-paragraph">The entry will be updated in the future.</p>



<p class="wp-block-paragraph" style="font-size:16px">Translated with <a href="https://www.deepl.com/es/translator/" target="_blank" rel="noreferrer noopener nofollow">DeepL</a>.</p>


<h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#zx-spectrum-model">ZX Spectrum model</a>
</li>
<li><a href="#pal-vs-ntsc">PAL vs NTSC</a>

<ul>
<li><a href="#what-is-the-difference">What is the difference?</a>
</li>
<li><a href="#how-does-it-affect-us">How does it affect us?</a>
</li>
<li><a href="#how-do-we-find-out-the-system">How do we find out the system?</a>
</li>
</ul>
</li>
<li><a href="#downloads">Downloads</a>
</li></ul>


<h2 id="zx-spectrum-model" class="wp-block-heading has-x-large-font-size">ZX Spectrum model</h2>



<p class="has-medium-font-size wp-block-paragraph">On more than one occasion we will need to know which model of ZX Spectrum our program is going to run on, 16K, 48K or 128K, and in the latter case if it is in 48K or 128K mode.</p>



<p class="has-medium-font-size wp-block-paragraph">The way to get the ZX Spectrum model I&#8217;m going to show is based on what <a href="https://twitter.com/sergio_thepope" target="_blank" rel="noreferrer noopener nofollow">Sergio thEpOpE</a> exposed in the <a href="https://t.me/CursoBasicZX" target="_blank" rel="noreferrer noopener nofollow">Basic ZX Course</a> group on Telegram.</p>



<p class="has-medium-font-size wp-block-paragraph">Depending on the value that we find in the memory position 0x4B (75), we can know if it is a 128K model or on the contrary if it is a 16/48K model. If when reading the value of this memory position we get 0x6E (110), the program is running in a 128K model, otherwise it is running in a 16/48K model.</p>



<p class="has-medium-font-size wp-block-paragraph">If you are running on a 16/48K ZX Spectrum model, it is interesting to know which one it is, information that can be found at locations 0x5CB4 (23,732) and 0x5CB5 (23,733). In these addresses we will find the total amount of memory: 0x7FFF (32,767) in 16K models or 0xFFFFFF (65,535) in 48K models. As we can see, the second byte (the least significant one) has the same value, changing only the most significant one, which is located at memory location 0x5CB5 (23,733). </p>



<p class="has-medium-font-size wp-block-paragraph">If we read the value of this position and get 0x7F (127) we are executing in a 16K model, if we get 0xFF (255) we are executing in a 48K model. For 128K models the value is the same as for 48K models.</p>



<p class="has-medium-font-size wp-block-paragraph">If it is running on a 128K ZX Spectrum model, it is possible that it is running in 48K or 128K mode, which we can also find out. First we get the value of the memory location 0x5C3B (23,611), we divide by 16 and the result is divided by 2. If the result of this operation is equal to the integer division of the value of the memory location by 16 and the result is divided by 2, it is running in 48K mode, otherwise in 128K mode.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">Pseudocode</span><span role="button" tabindex="0" data-code="IF INT(PEEK(23611)/16)/2 = INT(INT(PEEK(32611)/16/2)
    48K model
ELSE
    128K model
END IF" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">IF INT(PEEK(23611)/16)/2 = INT(INT(PEEK(32611)/16/2)</span></span>
<span class="line"><span style="color: #002339">    48K model</span></span>
<span class="line"><span style="color: #002339">ELSE</span></span>
<span class="line"><span style="color: #002339">    128K model</span></span>
<span class="line"><span style="color: #002339">END IF</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In other words, if the result of the integer division between the value of memory location 0x5C3B (23,611) and 16 is even, it is in 48K mode, otherwise it is in 128K mode.</p>



<p class="has-medium-font-size wp-block-paragraph">In ZX Spectrum <a href="https://espamatica.com/category/z80-assembly/">assembly</a> we are allowed, with right-hand rotations, to make divisions by powers of 2 (16 = 2^4), so we would have to make four rotations to make the division by 16 and then check if the result is even, bit 0 is zero.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $cc                   ; A = 1100 1100
; We use displacements to perform the division by 16
sra  a                        ; A = A/2 =  0110 0110
sra  a                        ; A = A/4 =  0011 0011
sra  a                        ; A = A/8 =  0001 1001
sra  a                        ; A = A/16 = 0000 1100
; Check if A is even
bit  $00, a
jr   z, isEven                ; If bit 0 is 0, it is even, it skips
isOdd:
; isOdd code
isEven:
; isEven code" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, $cc                   </span><span style="color: #357B42; font-style: italic">; A = 1100 1100</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; We use displacements to perform the division by 16</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/2 =  0110 0110</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/4 =  0011 0011</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/8 =  0001 1001</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/16 = 0000 1100</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Check if A is even</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$00</span><span style="color: #002339">, a</span></span>
<span class="line"><span style="color: #002339">jr   z, isEven                </span><span style="color: #357B42; font-style: italic">; If bit 0 is 0, it is even, it skips</span></span>
<span class="line"><span style="color: #7EB233">isOdd:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; isOdd code</span></span>
<span class="line"><span style="color: #7EB233">isEven:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; isEven code</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If you notice, we have shifted bits 4 to 7 to bits 0 to 3 and bits 4 to 7 have been set to zero, finally checking if bit 0, formerly bit 4, is 0 if it is even or 1 if it is odd. We can save displacements and check bit 4 directly.</p>



<p class="has-medium-font-size wp-block-paragraph">Based on what we have seen, the routine to find out on which ZX Spectrum model we are running a program could be the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ZXModel
;
; Gets the ZX model
;
; Entry:  nothing
;
; Output: BC = ZX model
;              0 = 16K
;              1 = 48K
;              2 = 128K
;              3 = 128K in 48K mode
; Alters the value of the BC and AF registers.
ZXModel:
ld   bc, $00			; BC is used to return the value to BASIC

ld   a, ($4b)           ; A = model address
cp   $6e
jr   z, is128           ; $6E, 128, jump

is16or48:
ld   a, ($5cb5)         ; A = amount of memory, upper part
rla                     ; Rotate to the right to check if it is $BF or $FF
ret  nc                 ; NC = 16K, jump ($BF)
inc  c                  ; C+=1
ret                     ; 48K, jump

is128:
ld   c, $02             ; C = 128K
ld   a, ($5c3b)         ; A = mode address
; IF INT(PEEK 23611/16)/2 = INT(INT(PEEK 23611/16)/2) = 48K.
; Dividing by 16 is like shifting 4 times to the right,
; bit 4 is set to bit 0
; Knowing if it is divisible by two is like knowing if it is even, bit 0 = 0
; It all boils down to evaluating bit 4, if it is 0 it is 48K mode, if it is 1 it is 128K mode
bit  $04, a             ; Bit 4?
ret  nz                 ; !0, 128K mode, exit
inc  c                  ; C+=1
ret                     ; 48K mode" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ZXModel</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Gets the ZX model</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entry:  nothing</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Output: BC = ZX model</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              0 = 16K</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              1 = 48K</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              2 = 128K</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              3 = 128K in 48K mode</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the BC and AF registers.</span></span>
<span class="line"><span style="color: #7EB233">ZXModel:</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$00</span><span style="color: #002339">			</span><span style="color: #357B42; font-style: italic">; BC is used to return the value to BASIC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">$4b</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = model address</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$6e</span></span>
<span class="line"><span style="color: #002339">jr   z, is128           </span><span style="color: #357B42; font-style: italic">; $6E, 128, jump</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">is16or48:</span></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">$5cb5</span><span style="color: #002339">)         </span><span style="color: #357B42; font-style: italic">; A = amount of memory, upper part</span></span>
<span class="line"><span style="color: #002339">rla                     </span><span style="color: #357B42; font-style: italic">; Rotate to the right to check if it is $BF or $FF</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  nc                 </span><span style="color: #357B42; font-style: italic">; NC = 16K, jump ($BF)</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                  </span><span style="color: #357B42; font-style: italic">; C+=1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; 48K, jump</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">is128:</span></span>
<span class="line"><span style="color: #002339">ld   c, </span><span style="color: #174781">$02</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; C = 128K</span></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">$5c3b</span><span style="color: #002339">)         </span><span style="color: #357B42; font-style: italic">; A = mode address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; IF INT(PEEK 23611/16)/2 = INT(INT(PEEK 23611/16)/2) = 48K.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Dividing by 16 is like shifting 4 times to the right,</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 4 is set to bit 0</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Knowing if it is divisible by two is like knowing if it is even, bit 0 = 0</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; It all boils down to evaluating bit 4, if it is 0 it is 48K mode, if it is 1 it is 128K mode</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$04</span><span style="color: #002339">, a             </span><span style="color: #357B42; font-style: italic">; Bit 4?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  nz                 </span><span style="color: #357B42; font-style: italic">; !0, 128K mode, exit</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                  </span><span style="color: #357B42; font-style: italic">; C+=1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; 48K mode</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">To test this routine, you must compile it and load it, for example, at memory address 0x7D00 (32,000), so that it will run on any ZX Spectrum model or clone. Once the routine is loaded, if you type PRINT USR 32000, the value corresponding to the ZX Spectrum model on which it is running will be printed on the screen.</p>



<p class="has-medium-font-size wp-block-paragraph">The value returned by an assembly program to the ZX Spectrum BASIC is done through the BC register.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cec57789&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cec57789" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="706" height="688" data-attachment-id="5398" data-permalink="https://espamatica.com/ensamblador-zx-spectrum-utilidades/zxmodel/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?fit=706%2C688&amp;ssl=1" data-orig-size="706,688" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="ZX Model" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?fit=706%2C688&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?resize=706%2C688&#038;ssl=1" alt="ZX Spectrum model" class="wp-image-5398" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?w=706&amp;ssl=1 706w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?resize=300%2C292&amp;ssl=1 300w" sizes="auto, (max-width: 706px) 100vw, 706px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum model</figcaption></figure>



<h2 id="pal-vs-ntsc" class="wp-block-heading has-x-large-font-size">PAL vs NTSC</h2>



<p class="has-medium-font-size wp-block-paragraph">Sometimes, we may be interested in knowing if the ZX Spectrum on which our program is running is PAL or NTSC system.</p>



<h3 id="what-is-the-difference" class="wp-block-heading has-large-font-size">What is the difference?</h3>



<p class="has-medium-font-size wp-block-paragraph">In PAL system there are 50 interruptions per second, while in NTSC system the number of interruptions per second is 60.</p>



<h3 id="how-does-it-affect-us" class="wp-block-heading has-large-font-size">How does it affect us?</h3>



<p class="has-medium-font-size wp-block-paragraph">It can affect us a lot. If our program relies on interrupts for timing, for example by executing the enemy movement routine every five interrupts, on a PAL system the enemy will move five times per second, while on NTSC it will move six times per second, resulting in faster enemy movement on NTSC than on PAL, which can affect gameplay.</p>



<h3 id="how-do-we-find-out-the-system" class="wp-block-heading has-large-font-size">How do we find out the system?</h3>



<p class="has-medium-font-size wp-block-paragraph">In this case I have relied on Habisoft&#8217;s Es.pectrum emulator, which you can download from <a href="https://habisoft.com/espectrum/" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<p class="has-medium-font-size wp-block-paragraph">This wonderful emulator emulates a wide variety of ZX Spectrum models and their clones, so it&#8217;s ideal for testing the program I&#8217;ve implemented to detect if it&#8217;s PAL or NTSC, and it&#8217;s also free.</p>



<p class="has-medium-font-size wp-block-paragraph">The program is simple, activate the ZX Spectrum interrupts in mode 2 and the first time the interrupt routine is executed it sets swCount to one to indicate that the counter variable in the main loop is being incremented. The second time the interrupt routine is executed it sets swCount to two to indicate that the program should exit. The value of counter is loaded into BC so that it can be obtained from Basic.</p>



<p class="has-medium-font-size wp-block-paragraph">In short, the program counts the number of times the main loop is executed between two interrupts. Based on the test results, we could say that NTSC yields values below 100, while PAL yields values above 100.</p>



<p class="has-medium-font-size wp-block-paragraph">All this has to be treated with great care, if you look at the results you can see values that are at the limit.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(3 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; Program to find out if it's NTSC or PAL
; Compile: pasmo --name PAL_NTSC --tapbas PAL_NTSC.asm PAL_NTSC.tap
; Once loaded, run PRINT USR 32000 to see the result
; Not compatible with 16K model

; Results of PRINT USR 32768 using emulator Es.pectrum
;
; https://habisoft.com/espectrum/
;
; Sinclair ZX Spectrum:
;       48/48+                  216
;       48 NTSC                  64
;       128                     230
;       48 ar                   ¿?
;       48 se                   215
; Timex:
;       TS 2068                  64
;       TC 2068                 216
;       TC 2048                 216
;       Komputer 2086           216
; Investrónica:
;       48+ es                  215
;       128+ es                 230
;       Inves+                  230
; Amstrad:
;       +2                      230
;       +2 es                   230
;       +2 fr                   230
;       +2 ar                   ¿?
;       +2A 4.0                 230
;       +2A 4.1                 230
;       +2A 4.0 es              230
;       +2A 4.1 es              230
;       +2A ar                  ¿?
;       +3 4.0                  230
;       +3 4.1                  230
;       +3 4.0 es               230
;       +3 4.1 es               230
; Pentagon:
;       Pentagon 128            241
;       Pentagon 512            241
;       Pentagon 1024 SL        227
; ZS Rechearch:
;       Leningrad               184
;       Scorpion ZS-256         151
;       Scorpion ZS-256 Turbo+  215
; Microdigital:
;       Tk90x pt-BR              73
;       Tk90x es-AR             233
;       Tk95  pt-BR              72
;       Tk95  es-AR             233
; ATM:
;       ATM-Turbo               215
;       ATM-Turbo 2+            176
; Otros:
;       Orel BK-08               ¿?
;       Dubna 48K                15
;       HC-91                   230
;       BK-001                  105
;       Foton-Ik03               ¿?
;       Alf TV Game (Elf)        ¿?
;       Vesta IK-30             184
; Mods:
;       +3e                     230
;       +3e es                  230
;       +2e                     230
;       +2e es                  230
;       Pentagon es             241
; Virtuales:
;       Spec256 48K             216
;       Spect256 128K           230
org  $8000

; Input: prepare for interruptions
main:
ld   hl, isr            ; HL = interrupt routine address
ld   ($feff), hl        ; Store in $feff the address
ld   a, $fe             ; of routine isr
ld   i, a               ; I = $fe
im   2                  ; Go to interrupt mode 2

; Loop, waits for swCount to be 1 to start counting
; When swCount is 2, exit loop and end of program
loop:
ld   a, (swCount)       ; A = indicator to know whether to count
or   a                  ; A = 0?
jr   z, loop            ; Z = yes, jump
cp   $02                ; A = 2?
jr   z, exit            ; Z = yes, jump

ld   hl, counter        ; HL = counter address
inc  (hl)               ; Counter+=1

jr   loop               ; Loop

; Exit from the programme
; Sets interrupts to mode 1
; Sets the counter to BC so that it can be retrieved
; from BASIC
exit:
im   1                  ; Go to interrupt mode 1
ld   b, $00
ld   a, (counter)
ld   c, a               ; BC = counter, to return the result
ret                     ; Back to BASIC

; Interruptions routine
; At each interrupt it increments swCount
isr:
push hl                 ; Preserves HL

ld   hl, swCount
inc  (hl)               ; Increases the indicator to know if counting

pop  hl                 ; Retrieves HL

ei                      ; Enables interruptions
ret                     ; Exit

; Vars
swCount:                ; Indicator to know whether to count
db $00
counter:                ; Counter
db $00" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; Program to find out if it&#39;s NTSC or PAL</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Compile: pasmo --name PAL_NTSC --tapbas PAL_NTSC.asm PAL_NTSC.tap</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Once loaded, run PRINT USR 32000 to see the result</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Not compatible with 16K model</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Results of PRINT USR 32768 using emulator Es.pectrum</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; https://habisoft.com/espectrum/</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sinclair ZX Spectrum:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48/48+                  216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48 NTSC                  64</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       128                     230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48 ar                   ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48 se                   215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Timex:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       TS 2068                  64</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       TC 2068                 216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       TC 2048                 216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Komputer 2086           216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Investrónica:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48+ es                  215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       128+ es                 230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Inves+                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Amstrad:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2                      230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2 es                   230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2 fr                   230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2 ar                   ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.0                 230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.1                 230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.0 es              230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.1 es              230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A ar                  ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.0                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.1                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.0 es               230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.1 es               230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Pentagon:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon 128            241</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon 512            241</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon 1024 SL        227</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ZS Rechearch:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Leningrad               184</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Scorpion ZS-256         151</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Scorpion ZS-256 Turbo+  215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Microdigital:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk90x pt-BR              73</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk90x es-AR             233</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk95  pt-BR              72</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk95  es-AR             233</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ATM:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       ATM-Turbo               215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       ATM-Turbo 2+            176</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Otros:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Orel BK-08               ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Dubna 48K                15</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       HC-91                   230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       BK-001                  105</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Foton-Ik03               ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Alf TV Game (Elf)        ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Vesta IK-30             184</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Mods:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3e                     230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3e es                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2e                     230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2e es                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon es             241</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Virtuales:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Spec256 48K             216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Spect256 128K           230</span></span>
<span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$8000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: prepare for interruptions</span></span>
<span class="line"><span style="color: #7EB233">main:</span></span>
<span class="line"><span style="color: #002339">ld   hl, isr            </span><span style="color: #357B42; font-style: italic">; HL = interrupt routine address</span></span>
<span class="line"><span style="color: #002339">ld   ($feff), hl        </span><span style="color: #357B42; font-style: italic">; Store in $feff the address</span></span>
<span class="line"><span style="color: #002339">ld   a, $fe             </span><span style="color: #357B42; font-style: italic">; of routine isr</span></span>
<span class="line"><span style="color: #002339">ld   i, a               </span><span style="color: #357B42; font-style: italic">; I = $fe</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">2</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Go to interrupt mode 2</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Loop, waits for swCount to be 1 to start counting</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; When swCount is 2, exit loop and end of program</span></span>
<span class="line"><span style="color: #7B30D0">loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld   a, (swCount)       </span><span style="color: #357B42; font-style: italic">; A = indicator to know whether to count</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                  </span><span style="color: #357B42; font-style: italic">; A = 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, </span><span style="color: #7B30D0">loop</span><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Z = yes, jump</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$02</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2?</span></span>
<span class="line"><span style="color: #002339">jr   z, exit            </span><span style="color: #357B42; font-style: italic">; Z = yes, jump</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, counter        </span><span style="color: #357B42; font-style: italic">; HL = counter address</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)               </span><span style="color: #357B42; font-style: italic">; Counter+=1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">jr   </span><span style="color: #7B30D0">loop</span><span style="color: #002339">               </span><span style="color: #357B42; font-style: italic">; Loop</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Exit from the programme</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sets interrupts to mode 1</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sets the counter to BC so that it can be retrieved</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; from BASIC</span></span>
<span class="line"><span style="color: #7EB233">exit:</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">1</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Go to interrupt mode 1</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #002339">ld   a, (counter)</span></span>
<span class="line"><span style="color: #002339">ld   c, a               </span><span style="color: #357B42; font-style: italic">; BC = counter, to return the result</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Back to BASIC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Interruptions routine</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; At each interrupt it increments swCount</span></span>
<span class="line"><span style="color: #7EB233">isr:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl                 </span><span style="color: #357B42; font-style: italic">; Preserves HL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, swCount</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)               </span><span style="color: #357B42; font-style: italic">; Increases the indicator to know if counting</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl                 </span><span style="color: #357B42; font-style: italic">; Retrieves HL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ei                      </span><span style="color: #357B42; font-style: italic">; Enables interruptions</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Exit</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Vars</span></span>
<span class="line"><span style="color: #7EB233">swCount:</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; Indicator to know whether to count</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #7EB233">counter:</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; Counter</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span></code></pre></div>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cec58933&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cec58933" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="642" height="556" data-attachment-id="5405" data-permalink="https://espamatica.com/ensamblador-zx-spectrum-utilidades/pal_ntsc/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?fit=642%2C556&amp;ssl=1" data-orig-size="642,556" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="PAL_NTSC" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum &amp;#8211; PAL vs NTSC&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?fit=642%2C556&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?resize=642%2C556&#038;ssl=1" alt="ZX Spectrum - PAL vs NTSC" class="wp-image-5405" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?w=642&amp;ssl=1 642w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?resize=300%2C260&amp;ssl=1 300w" sizes="auto, (max-width: 642px) 100vw, 642px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum &#8211; PAL vs NTSC</figcaption></figure>



<h2 id="downloads" class="wp-block-heading has-x-large-font-size">Downloads</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="http://tinyurl.com/22srfwmz" rel="nofollow noopener" target="_blank">ZX Spectrum model</a></li>



<li class="has-medium-font-size"><a href="http://tinyurl.com/22qwfmwf" target="_blank" rel="noreferrer noopener nofollow">PAL vs NTSC</a></li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-utilities/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5490</post-id>	</item>
		<item>
		<title>Ensamblador ZX Spectrum &#8211; Utilidades</title>
		<link>https://espamatica.com/ensamblador-zx-spectrum-utilidades/</link>
					<comments>https://espamatica.com/ensamblador-zx-spectrum-utilidades/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Sun, 11 Feb 2024 08:00:00 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Ensamblador Z80]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Ensamblador]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=5391</guid>

					<description><![CDATA[Esta es una recopilación de distintas rutinas que pueden ser útiles para tratar determinados aspectos del ZX Spectrum. La entrada]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">Esta es una recopilación de distintas rutinas que pueden ser útiles para tratar determinados aspectos del ZX Spectrum.</p>



<p class="has-medium-font-size wp-block-paragraph">La entrada irá recibiendo actualizaciones.</p>


<h2 class="simpletoc-title">Tabla de contenidos</h2>
<ul class="simpletoc-list">
<li><a href="#modelo-de-zx-spectrum">Modelo de ZX Spectrum</a>
</li>
<li><a href="#pal-o-ntsc">PAL o NTSC</a>

<ul>
<li><a href="#que-diferencia-hay">¿Qué diferencia hay?</a>
</li>
<li><a href="#como-nos-afecta">¿Cómo nos afecta?</a>
</li>
<li><a href="#como-averiguamos-el-sistema">¿Cómo averiguamos el sistema?</a>
</li>
</ul>
</li>
<li><a href="#descargas">Descargas</a>
</li></ul>


<h2 id="modelo-de-zx-spectrum" class="wp-block-heading has-x-large-font-size">Modelo de ZX Spectrum</h2>



<p class="has-medium-font-size wp-block-paragraph">En más de una ocasión necesitaremos saber en que modelo de ZX Spectrum se va a ejecutar nuestro programa, 16K, 48K o 128K, y en éste último caso si está en modo 48K o 128K.</p>



<p class="has-medium-font-size wp-block-paragraph">La manera de obtener el modelo de ZX Spectrum que voy a mostrar se basa en lo expuesto por <a href="https://twitter.com/sergio_thepope" target="_blank" rel="noreferrer noopener nofollow">Sergio thEpOpE</a> en el grupo de <a href="https://t.me/CursoBasicZX" target="_blank" rel="noreferrer noopener nofollow">Curso Basic ZX</a> de Telegram.</p>



<p class="has-medium-font-size wp-block-paragraph">Según el valor que encontremos en la posición de memoria 0x4B (75), podemos saber si se trata de un modelo 128K o por el contrario si es un modelo de 16/48K. Si al leer el valor de esta posición de memoria obtenemos 0x6E (110), el programa se está ejecutando en un modelo de 128K, de lo contrario lo hace en un modelo 16/48K.</p>



<p class="has-medium-font-size wp-block-paragraph">Si se está ejecutando en un modelo de ZX Spectrum de 16/48K, es interesante saber en cuál de ellos es, información que encontramos en las posiciones 0x5CB4 (23.732) y 0x5CB5 (23.733). En estas direcciones encontraremos la cantidad total de memoria: 0x7FFF (32.767) en modelos de 16K o 0xFFFF (65.535) en modelos de 48K. Como vemos, el segundo byte (el menos significativo) tiene el mismo valor, cambiando solo el más significativo, que se encuentra en la posición de memoria 0x5CB5 (23.733). Si leemos el valor de esta posición y obtenemos 0x7F (127) estamos ejecutando en un modelo de 16K, si obtenemos 0xFF (255) lo estamos haciendo en uno de 48K.</p>



<p class="has-medium-font-size wp-block-paragraph">En los modelos de 128K el valor es el mismo que en los modelos de 48K.</p>



<p class="has-medium-font-size wp-block-paragraph">Si se está ejecutando en un modelo de ZX Spectrum de 128K, es posible que se esté ejecutando en modo 48K o 128K, lo cual también podemos saber. Primero obtenemos el valor de la posición de memoria 0x5C3B (23.611), hacemos la división entre 16 y el resultado lo dividimos entre 2. Si el resultado de esta operación es igual a hacer la división entera del valor de la posición de memoria entre 16 y al resultado hacerle la división entera entre 2, se estaría ejecutando en modo 48K, de lo contrario en modo 128K.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">Pseudocódigo</span><span role="button" tabindex="0" data-code="SI INT(PEEK(23611)/16)/2 = INT(INT(PEEK(32611)/16/2)
    modo 48K
SI NO
    modo 128K
FIN SI" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">SI INT(PEEK(23611)/16)/2 = INT(INT(PEEK(32611)/16/2)</span></span>
<span class="line"><span style="color: #002339">    modo 48K</span></span>
<span class="line"><span style="color: #002339">SI NO</span></span>
<span class="line"><span style="color: #002339">    modo 128K</span></span>
<span class="line"><span style="color: #002339">FIN SI</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">O lo que es lo mismo, si el resultado de la división entera entre el valor de la posición de memoria 0x5C3B (23.611) y 16 es par está en modo 48K, de lo contrario está en modo 128K.</p>



<p class="has-medium-font-size wp-block-paragraph">En <a href="https://espamatica.com/category/ensambladorz80/">ensamblador</a> para ZX Spectrum se nos permite, con rotaciones hacia la derecha, hacer divisiones entre potencias de 2 (16 = 2^4), por lo que tendríamos que hacer cuatro rotaciones para realizar la división entre 16 y luego comprobar si es resultado es par, el bit 0 está a cero.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $cc                   ; A = 1100 1100
; Usamos desplazamientos para realizar la división entre 16
sra  a                        ; A = A/2 =  0110 0110
sra  a                        ; A = A/4 =  0011 0011
sra  a                        ; A = A/8 =  0001 1001
sra  a                        ; A = A/16 = 0000 1100
; Comprueba si A es par
bit  $00, a
jr   z, EsPar                 ; Si el bit 0 es 0, es par, salta
EsImpar:
; Código de EsImpar
EsPar:
; Código de EsPar" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, $cc                   </span><span style="color: #357B42; font-style: italic">; A = 1100 1100</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Usamos desplazamientos para realizar la división entre 16</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/2 =  0110 0110</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/4 =  0011 0011</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/8 =  0001 1001</span></span>
<span class="line"><span style="color: #002339">sra  a                        </span><span style="color: #357B42; font-style: italic">; A = A/16 = 0000 1100</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Comprueba si A es par</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$00</span><span style="color: #002339">, a</span></span>
<span class="line"><span style="color: #002339">jr   z, EsPar                 </span><span style="color: #357B42; font-style: italic">; Si el bit 0 es 0, es par, salta</span></span>
<span class="line"><span style="color: #7EB233">EsImpar:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Código de EsImpar</span></span>
<span class="line"><span style="color: #7EB233">EsPar:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Código de EsPar</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Si os fijáis, hemos desplazado los bits 4 a 7 hasta los bits 0 a 3 y los 4 a 7 se han puesto a cero, comprobando finalmente si el bit 0, anteriormente bit 4, es 0 en el caso de ser par o 1 en el caso de ser impar. Nos podemos ahorrar desplazamientos y comprobar directamente el bit 4.</p>



<p class="has-medium-font-size wp-block-paragraph">En base a lo que hemos visto, la rutina para averiguar en qué modelo de ZX Spectrum estamos ejecutando un programa podría ser la siguiente:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ZXModel
;
; Obtiene el modelo de ZX
;
; Entrada: nada
;
; Salida:  BC = modelo de ZX
;              0 = 16K
;              1 = 48K
;              2 = 128K
;              3 = 128K en modo 48K
; Altera el valor de los registros BC y AF.
ZXModel:
ld   bc, $00			; BC se utiliza para devolver el valor a BASIC

ld   a, ($4b)           ; A = dirección modelo
cp   $6e
jr   z, is128           ; $6E, 128, salta

is16or48:
ld   a, ($5cb5)         ; A = cantidad memoria, parte alta
rla                     ; Rota a la derecha para comprobar si es $BF o $FF
ret  nc                 ; NC = 16K, sale ($BF)
inc  c                  ; C+=1
ret                     ; 48K, sale

is128:
ld   c, $02             ; C = 128K
ld   a, ($5c3b)         ; A = dirección de modo
; Sí INT(PEEK 23611/16)/2 = INT(INT(PEEK 23611/16)/2) = 48K.
; Dividir ente 16 es como desplazar 4 veces hacia de derecha,
; el bit 4 pasa al bit 0. 
; Saber si es divisible entre dos es como saber si es par, el bit 0 = 0
; Todo se resume en evaluar el bit 4, si es 0 es modo 48K, si es 1 es modo 128K.
bit  $04, a             ; ¿Bit 4?
ret  nz                 ; !0, modo 128K, sale
inc  c                  ; C+=1
ret                     ; Modo 48K" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ZXModel</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Obtiene el modelo de ZX</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: nada</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida:  BC = modelo de ZX</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              0 = 16K</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              1 = 48K</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              2 = 128K</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;              3 = 128K en modo 48K</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Altera el valor de los registros BC y AF.</span></span>
<span class="line"><span style="color: #7EB233">ZXModel:</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$00</span><span style="color: #002339">			</span><span style="color: #357B42; font-style: italic">; BC se utiliza para devolver el valor a BASIC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">$4b</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = dirección modelo</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$6e</span></span>
<span class="line"><span style="color: #002339">jr   z, is128           </span><span style="color: #357B42; font-style: italic">; $6E, 128, salta</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">is16or48:</span></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">$5cb5</span><span style="color: #002339">)         </span><span style="color: #357B42; font-style: italic">; A = cantidad memoria, parte alta</span></span>
<span class="line"><span style="color: #002339">rla                     </span><span style="color: #357B42; font-style: italic">; Rota a la derecha para comprobar si es $BF o $FF</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  nc                 </span><span style="color: #357B42; font-style: italic">; NC = 16K, sale ($BF)</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                  </span><span style="color: #357B42; font-style: italic">; C+=1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; 48K, sale</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">is128:</span></span>
<span class="line"><span style="color: #002339">ld   c, </span><span style="color: #174781">$02</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; C = 128K</span></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">$5c3b</span><span style="color: #002339">)         </span><span style="color: #357B42; font-style: italic">; A = dirección de modo</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sí INT(PEEK 23611/16)/2 = INT(INT(PEEK 23611/16)/2) = 48K.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Dividir ente 16 es como desplazar 4 veces hacia de derecha,</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; el bit 4 pasa al bit 0. </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Saber si es divisible entre dos es como saber si es par, el bit 0 = 0</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Todo se resume en evaluar el bit 4, si es 0 es modo 48K, si es 1 es modo 128K.</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$04</span><span style="color: #002339">, a             </span><span style="color: #357B42; font-style: italic">; ¿Bit 4?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  nz                 </span><span style="color: #357B42; font-style: italic">; !0, modo 128K, sale</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                  </span><span style="color: #357B42; font-style: italic">; C+=1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Modo 48K</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Para probar esta rutina, deberéis compilarla y cargarla, por ejemplo, en la dirección de memoria 0x7D00 (32.000), para que corra en cualquier modelo o clon de ZX Spectrum. Una vez cargada la rutina, si tecleáis PRINT USR 32000 saldrá impreso en pantalla el valor correspondiente al modelo de ZX Spectrum en el que se está ejecutando.</p>



<p class="has-medium-font-size wp-block-paragraph">El valor devuelto por un programa ensamblador al BASIC del ZX Spectrum se hace a través del registro BC.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cec63cf8&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cec63cf8" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="706" height="688" data-attachment-id="5398" data-permalink="https://espamatica.com/ensamblador-zx-spectrum-utilidades/zxmodel/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?fit=706%2C688&amp;ssl=1" data-orig-size="706,688" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="ZX Model" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?fit=706%2C688&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?resize=706%2C688&#038;ssl=1" alt="Modelo ZX Spectrum" class="wp-image-5398" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?w=706&amp;ssl=1 706w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/ZXModel.png?resize=300%2C292&amp;ssl=1 300w" sizes="auto, (max-width: 706px) 100vw, 706px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">Modelo de ZX Spectrum</figcaption></figure>



<h2 id="pal-o-ntsc" class="wp-block-heading has-x-large-font-size">PAL o NTSC</h2>



<p class="has-medium-font-size wp-block-paragraph">En ocasiones, nos puede interesar saber si el ZX Spectrum en el que se está ejecutando nuestro programa es sistema PAL o NTSC. </p>



<h3 id="que-diferencia-hay" class="wp-block-heading has-large-font-size">¿Qué diferencia hay?</h3>



<p class="has-medium-font-size wp-block-paragraph">En sistema PAL se producen 50 interrupciones por segundo, mientras que en sistema NTSC el número de interrupciones por segundo asciende a 60.</p>



<h3 id="como-nos-afecta" class="wp-block-heading has-large-font-size">¿Cómo nos afecta? </h3>



<p class="has-medium-font-size wp-block-paragraph">Nos puede llegar a afectar y mucho. Si nuestro programa se apoya en las interrupciones para temporizar, por ejemplo ejecutando la rutina de movimiento del enemigo cada cinco interrupciones, en un sistema PAL el enemigo se moverá cinco veces por segundo, mientras que en NTSC se moverá seis veces por segundo, resultando el movimiento del enemigo más rápido en NTSC que en PAL, pudiendo influir en la jugabilidad.</p>



<h3 id="como-averiguamos-el-sistema" class="wp-block-heading has-large-font-size">¿Cómo averiguamos el sistema?</h3>



<p class="has-medium-font-size wp-block-paragraph">En este caso me he apoyado en el emulador Es.pectrum de Habisoft, que podéis descargar desde <a href="https://habisoft.com/espectrum/" target="_blank" rel="noreferrer noopener nofollow">aquí</a>.</p>



<p class="has-medium-font-size wp-block-paragraph">Este maravilloso emulador emula una vasta variedad de modelos de ZX Spectrum y sus clones, por lo que es ideal para probar el programa que he implementado para detectar si es PAL o NTSC, y además es gratuito.</p>



<p class="has-medium-font-size wp-block-paragraph">El programa es sencillo, activa la interrupciones del ZX Spectrum en modo 2 y la primera vez que se ejecuta la rutina de interrupciones pone swCount a uno para indicar que se vaya incrementando la variable counter en el bucle principal. La segunda vez que se ejecuta la rutina de interrupciones pone swCount a dos para indicar que se debe salir el programa. El valor de counter se carga en BC para que se pueda obtener desde Basic.</p>



<p class="has-medium-font-size wp-block-paragraph">En resumen, el programa cuenta las veces que se ejecuta el bucle principal entre dos interrupciones. En base a los resultados arrojados por la pruebas, podríamos decir que NTSC arroja valores inferiores a 100, mientras que PAL los arroja superiores.</p>



<p class="has-medium-font-size wp-block-paragraph">Todo esto hay que tratarlo con mucho cuidado, si observáis los resultados podéis ver valores que están al límite.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(3 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; Programa para averiguar si es NTSC o PAL
; Compilar: pasmo --name PAL_NTSC --tapbas PAL_NTSC.asm PAL_NTSC.tap
; Una vez cargado, ejecutar PRINT USR 32000 para ver el resultado
; No compatible con modelo de 16K

; Resultados de PRINT USR 32000 usando emulador Es.pectrum
;
; https://habisoft.com/espectrum/
;
; Sinclair:
;       48/48+                  216
;       48 NTSC                  64
;       128                     230
;       48 ar                   ¿?
;       48 se                   215
; Timex:
;       TS 2068                  64
;       TC 2068                 216
;       TC 2048                 216
;       Komputer 2086           216
; Investrónica:
;       48+ es                  215
;       128+ es                 230
;       Inves+                  230
; Amstrad:
;       +2                      230
;       +2 es                   230
;       +2 fr                   230
;       +2 ar                   ¿?
;       +2A 4.0                 230
;       +2A 4.1                 230
;       +2A 4.0 es              230
;       +2A 4.1 es              230
;       +2A ar                  ¿?
;       +3 4.0                  230
;       +3 4.1                  230
;       +3 4.0 es               230
;       +3 4.1 es               230
; Pentagon:
;       Pentagon 128            241
;       Pentagon 512            241
;       Pentagon 1024 SL        227
; ZS Rechearch:
;       Leningrad               184
;       Scorpion ZS-256         151
;       Scorpion ZS-256 Turbo+  215
; Microdigital:
;       Tk90x pt-BR              73
;       Tk90x es-AR             233
;       Tk95  pt-BR              72
;       Tk95  es-AR             233
; ATM:
;       ATM-Turbo               215
;       ATM-Turbo 2+            176
; Otros:
;       Orel BK-08               ¿?
;       Dubna 48K                15
;       HC-91                   230
;       BK-001                  105
;       Foton-Ik03               ¿?
;       Alf TV Game (Elf)        ¿?
;       Vesta IK-30             184
; Mods:
;       +3e                     230
;       +3e es                  230
;       +2e                     230
;       +2e es                  230
;       Pentagon es             241
; Virtuales:
;       Spec256 48K             216
;       Spect256 128K           230
org  $8000

; Entrada: prepara las interrupciones
main:
ld   hl, isr            ; HL = dirección rutina interrupciones
ld   ($feff), hl        ; Guarda en $feff la dirección
ld   a, $fe             ; de la rutina isr
ld   i, a               ; I = $fe
im   2                  ; Pasa a modo 2 de interrupciones

; Bucle, espera a que swCount sea 1 para empezar a contar
; Cuando swCount es 2, sale del bucle y fin de programa
loop:
ld   a, (swCount)       ; A = indicador para saber si contar
or   a                  ; ¿A = 0?
jr   z, loop            ; Z = sí, salta
cp   $02                ; ¿A = 2?
jr   z, exit            ; Z = sí, salta

ld   hl, counter        ; HL = dirección contador
inc  (hl)               ; Contador+=1

jr   loop               ; Bucle

; Salida del programa
; Pone las interrupciones en modo 1
; Pone el contador en BC para que pueda ser recuperado
; desde BASIC
exit:
im   1                  ; Pasa a modo 1 de interrupciones
ld   b, $00
ld   a, (counter)
ld   c, a               ; BC = contador, para devolver el resultado
ret                     ; Vuelta al BASIC

; Rutina de interrupciones
; En cada interrupción incrementa swCount
isr:
push hl                 ; Preserva HL

ld   hl, swCount
inc  (hl)               ; Incrementa el indicador para saber si contar

pop  hl                 ; Recupera HL

ei                      ; Habilita interrupciones
ret                     ; Sale

; Variables
swCount:                ; Indicador para saber si contar
db $00
counter:                ; Contador
db $00" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; Programa para averiguar si es NTSC o PAL</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Compilar: pasmo --name PAL_NTSC --tapbas PAL_NTSC.asm PAL_NTSC.tap</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Una vez cargado, ejecutar PRINT USR 32000 para ver el resultado</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; No compatible con modelo de 16K</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Resultados de PRINT USR 32000 usando emulador Es.pectrum</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; https://habisoft.com/espectrum/</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sinclair:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48/48+                  216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48 NTSC                  64</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       128                     230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48 ar                   ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48 se                   215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Timex:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       TS 2068                  64</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       TC 2068                 216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       TC 2048                 216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Komputer 2086           216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Investrónica:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       48+ es                  215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       128+ es                 230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Inves+                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Amstrad:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2                      230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2 es                   230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2 fr                   230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2 ar                   ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.0                 230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.1                 230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.0 es              230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A 4.1 es              230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2A ar                  ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.0                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.1                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.0 es               230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3 4.1 es               230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Pentagon:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon 128            241</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon 512            241</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon 1024 SL        227</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ZS Rechearch:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Leningrad               184</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Scorpion ZS-256         151</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Scorpion ZS-256 Turbo+  215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Microdigital:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk90x pt-BR              73</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk90x es-AR             233</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk95  pt-BR              72</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Tk95  es-AR             233</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ATM:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       ATM-Turbo               215</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       ATM-Turbo 2+            176</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Otros:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Orel BK-08               ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Dubna 48K                15</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       HC-91                   230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       BK-001                  105</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Foton-Ik03               ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Alf TV Game (Elf)        ¿?</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Vesta IK-30             184</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Mods:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3e                     230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +3e es                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2e                     230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       +2e es                  230</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Pentagon es             241</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Virtuales:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Spec256 48K             216</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;       Spect256 128K           230</span></span>
<span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$8000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: prepara las interrupciones</span></span>
<span class="line"><span style="color: #7EB233">main:</span></span>
<span class="line"><span style="color: #002339">ld   hl, isr            </span><span style="color: #357B42; font-style: italic">; HL = dirección rutina interrupciones</span></span>
<span class="line"><span style="color: #002339">ld   ($feff), hl        </span><span style="color: #357B42; font-style: italic">; Guarda en $feff la dirección</span></span>
<span class="line"><span style="color: #002339">ld   a, $fe             </span><span style="color: #357B42; font-style: italic">; de la rutina isr</span></span>
<span class="line"><span style="color: #002339">ld   i, a               </span><span style="color: #357B42; font-style: italic">; I = $fe</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">2</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Pasa a modo 2 de interrupciones</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Bucle, espera a que swCount sea 1 para empezar a contar</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Cuando swCount es 2, sale del bucle y fin de programa</span></span>
<span class="line"><span style="color: #7B30D0">loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld   a, (swCount)       </span><span style="color: #357B42; font-style: italic">; A = indicador para saber si contar</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                  </span><span style="color: #357B42; font-style: italic">; ¿A = 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, </span><span style="color: #7B30D0">loop</span><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Z = sí, salta</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$02</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; ¿A = 2?</span></span>
<span class="line"><span style="color: #002339">jr   z, exit            </span><span style="color: #357B42; font-style: italic">; Z = sí, salta</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, counter        </span><span style="color: #357B42; font-style: italic">; HL = dirección contador</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)               </span><span style="color: #357B42; font-style: italic">; Contador+=1</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">jr   </span><span style="color: #7B30D0">loop</span><span style="color: #002339">               </span><span style="color: #357B42; font-style: italic">; Bucle</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida del programa</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Pone las interrupciones en modo 1</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Pone el contador en BC para que pueda ser recuperado</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; desde BASIC</span></span>
<span class="line"><span style="color: #7EB233">exit:</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">1</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Pasa a modo 1 de interrupciones</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #002339">ld   a, (counter)</span></span>
<span class="line"><span style="color: #002339">ld   c, a               </span><span style="color: #357B42; font-style: italic">; BC = contador, para devolver el resultado</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Vuelta al BASIC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Rutina de interrupciones</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; En cada interrupción incrementa swCount</span></span>
<span class="line"><span style="color: #7EB233">isr:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl                 </span><span style="color: #357B42; font-style: italic">; Preserva HL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, swCount</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)               </span><span style="color: #357B42; font-style: italic">; Incrementa el indicador para saber si contar</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl                 </span><span style="color: #357B42; font-style: italic">; Recupera HL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ei                      </span><span style="color: #357B42; font-style: italic">; Habilita interrupciones</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Sale</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Variables</span></span>
<span class="line"><span style="color: #7EB233">swCount:</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; Indicador para saber si contar</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #7EB233">counter:</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; Contador</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span></code></pre></div>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cec648e7&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cec648e7" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="642" height="556" data-attachment-id="5405" data-permalink="https://espamatica.com/ensamblador-zx-spectrum-utilidades/pal_ntsc/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?fit=642%2C556&amp;ssl=1" data-orig-size="642,556" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="PAL_NTSC" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum &amp;#8211; PAL vs NTSC&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?fit=642%2C556&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?resize=642%2C556&#038;ssl=1" alt="ZX Spectrum - PAL vs NTSC" class="wp-image-5405" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?w=642&amp;ssl=1 642w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/PAL_NTSC.png?resize=300%2C260&amp;ssl=1 300w" sizes="auto, (max-width: 642px) 100vw, 642px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum &#8211; PAL o NTSC</figcaption></figure>



<h2 id="descargas" class="wp-block-heading has-x-large-font-size">Descargas</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="http://tinyurl.com/22srfwmz" target="_blank" rel="noreferrer noopener nofollow">Modelo ZX Spectrum</a></li>



<li class="has-medium-font-size"><a href="http://tinyurl.com/22qwfmwf" target="_blank" rel="noreferrer noopener sponsored nofollow">PAL o NTSC</a></li>
</ul>



<p class="wp-block-paragraph"></p>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/ensamblador-zx-spectrum-utilidades/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5391</post-id>	</item>
		<item>
		<title>Boriel Basic para ZX Spectrum</title>
		<link>https://espamatica.com/boriel-basic-para-zx-spectrum/</link>
					<comments>https://espamatica.com/boriel-basic-para-zx-spectrum/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Thu, 08 Feb 2024 04:43:02 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Boriel Basic]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Boriel]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=5433</guid>

					<description><![CDATA[Bajo el título Boriel Basic para ZX Spectrum. Manual para torpes… y para los que no lo son tanto, Juan]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">Bajo el título <strong>Boriel Basic para ZX Spectrum. Manual para torpes… y para los que no lo son tanto</strong>, Juan Segura Durán (aka Duefectu) nos trae un libro con el que nos promete, con algo de esfuerzo y dedicación por nuestra parte, dominar todo los entresijos de Boriel Basic para así sacarle el máximo en nuestros desarrollos para ZX Spectrum, ya sea clásico o ZX Spectrum Next. Si, habéis leído bien, ZX Spectrum Next. </p>



<p class="has-medium-font-size wp-block-paragraph">A través de quince capítulos y cinco anexos, nos guía en el aprendizaje de este potente lenguaje basado en el Basic de Sinclair, pero que, a diferencia de éste, vuela, de manera que la consabida lentitud del Basic parece más una leyenda urbana que una triste realidad.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="380" height="630" data-attachment-id="5431" data-permalink="https://espamatica.com/borielbasiclibro/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicLibro.png?fit=380%2C630&amp;ssl=1" data-orig-size="380,630" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic" data-image-description="" data-image-caption="&lt;p&gt;Boriel Basic &lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicLibro.png?fit=380%2C630&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicLibro.png?resize=380%2C630&#038;ssl=1" alt="Boriel Basic" class="wp-image-5431" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicLibro.png?w=380&amp;ssl=1 380w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicLibro.png?resize=181%2C300&amp;ssl=1 181w" sizes="auto, (max-width: 380px) 100vw, 380px" /><figcaption class="wp-element-caption">Boriel Basic </figcaption></figure>


<h2 class="simpletoc-title">Tabla de contenidos</h2>
<ul class="simpletoc-list">
<li><a href="#que-es-boriel-basic">¿Qué es Boriel Basic?</a>
</li>
<li><a href="#como-esta-distribuido-el-libro">¿Cómo está distribuido el libro?</a>

<ul>
<li><a href="#1-introduccion">1. Introducción</a>
</li>
<li><a href="#2-calentando-motores">2. Calentando motores</a>
</li>
<li><a href="#3-el-compilador-boriel-basic">3. El compilador Boriel Basic</a>
</li>
<li><a href="#4-variables-y-tipos">4. Variables y tipos</a>
</li>
<li><a href="#5-el-lenguaje-boriel-basic">5. El lenguaje Boriel Basic</a>
</li>
<li><a href="#6-la-memoria">6. La memoria</a>
</li>
<li><a href="#7-la-pantalla-en-el-zx-spectrum">7. La pantalla en el ZX Spectrum</a>
</li>
<li><a href="#8-la-pantalla-en-el-next-ngo">8. La pantalla en el Next / N-GO</a>
</li>
<li><a href="#9-colisiones">9. Colisiones</a>
</li>
<li><a href="#10-controlando-el-spectrum">10. Controlando el Spectrum</a>
</li>
<li><a href="#11-las-interrupciones">11. Las interrupciones</a>
</li>
<li><a href="#12-el-beeper">12. El Beeper</a>
</li>
<li><a href="#13-el-chip-ay38912">13. El chip AY-3-8912</a>
</li>
<li><a href="#14-arquitectura">14. Arquitectura</a>
</li>
<li><a href="#15-creando-el-producto-final">15. Creando el producto final</a>
</li>
</ul>
</li>
<li><a href="#anexos">Anexos</a>
</li>
<li><a href="#recursos">Recursos</a>
</li>
<li><a href="#como-obtengo-el-libro">¿Cómo obtengo el libro?</a>
</li>
<li><a href="#el-autor">El autor</a>
</li>
<li><a href="#conclusion">Conclusión</a>
</li></ul>


<h2 id="que-es-boriel-basic" class="wp-block-heading has-large-font-size">¿Qué es Boriel Basic?</h2>



<p class="has-medium-font-size wp-block-paragraph">Boriel Basic es lo que se denomina un Basic compilado, desarrollado por José Rodríguez (aka Boriel), pero que no se limita a traducir el Basic de Sinclair a código máquina. José Rodríguez ha reescrito gran parte de las instrucciones del Basic de Sinclair en código máquina, por lo que el comando PRINT hace lo que pensaron los ingenieros de Sinclair, pero no de la misma manera, con Boriel Basic es mucho más rápido y óptimo. Además, ha modernizado la sintaxis para incorporar mecanismos de lenguajes modernos, lo que permite al programador estructurar y reutilizar su código de forma sencilla.</p>



<p class="has-medium-font-size wp-block-paragraph">Boriel Basic tiene un compilador propio que traduce el código fuente a código ensamblador y de éste a código máquina. Además, dispone de muchas librerías; cabe destacar las de scroll al carácter y al píxel, e impresión de sprites acelerada, por lo que no tenemos que inventar la rueda, solo usarla.</p>



<p class="has-medium-font-size wp-block-paragraph">Así mismo, y dado que la capacidad de memoria en los equipos a los que van a ir dirigidos nuestros programas es muy limitada, el compilador incluye un optimizador que, aparte de acelerar ciertas partes, solo incluye las rutinas a las que se llama, no toda la librería.</p>



<p class="has-medium-font-size wp-block-paragraph">Todo esto lo puedes aprender leyendo el libro que nos ocupa.</p>



<h2 id="como-esta-distribuido-el-libro" class="wp-block-heading has-large-font-size">¿Cómo está distribuido el libro?</h2>



<p class="has-medium-font-size wp-block-paragraph">El libro consta de quince capítulos y cinco anexos a través de los cuales se va desgranando Boriel Basic, de tal manera que al finalizar la lectura del mismo serás capaz de realizar tus propios desarrollos para ZX Spectrum, e incluso librerías para ampliar las posibilidades de Boriel.</p>



<p class="has-medium-font-size wp-block-paragraph">También consta de cuatro prólogos a cargo de: Juan Antonio Rubio García «Juanan Homospectrum», José Rodríguez «Boriel», Javier Ortíz Carrasco «El Spectrumero» y Juan Segura Durán «Duefectu».</p>



<p class="has-medium-font-size wp-block-paragraph">En cada uno de los capítulos encontrarás un código QR y una URL para poder descargar todo el código fuente desarrollado en el mismo.</p>



<p class="has-medium-font-size wp-block-paragraph">A continuación, una pequeña descripción sobre lo que vais a encontrar en cada capítulo.</p>



<h3 id="1-introduccion" class="wp-block-heading has-large-font-size">1. Introducción</h3>



<p class="has-medium-font-size wp-block-paragraph">En este capítulo encontramos los agradecimientos, cuáles son los objetivos del libro, tipo de lector al qué va dirigido y una serie de conceptos que se dan por asumidos por parte del lector; conocimientos que son necesarios que tenga para poder aprovechar en su totalidad la lectura del libro.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="168" height="306" data-attachment-id="5416" data-permalink="https://espamatica.com/borielbasiccap01/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap01.png?fit=168%2C306&amp;ssl=1" data-orig-size="168,306" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 1" data-image-description="" data-image-caption="&lt;p&gt;Capítulo 1&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap01.png?fit=168%2C306&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap01.png?resize=168%2C306&#038;ssl=1" alt="Boriel Basic - Capítulo 1" class="wp-image-5416" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap01.png?w=168&amp;ssl=1 168w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap01.png?resize=165%2C300&amp;ssl=1 165w" sizes="auto, (max-width: 168px) 100vw, 168px" /><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 1</figcaption></figure>



<h3 id="2-calentando-motores" class="wp-block-heading has-large-font-size">2. Calentando motores</h3>



<p class="has-medium-font-size wp-block-paragraph">En este capítulo se detallan las herramientas necesarias para seguir el libro: distintos IDE que podemos usar, emuladores, herramientas gráficas, para sonido y otras utilidades.</p>



<p class="has-medium-font-size wp-block-paragraph">De cada una de las herramientas se proporciona la URL para su descarga y/o uso en el caso de las herramientas online.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="195" height="433" data-attachment-id="5417" data-permalink="https://espamatica.com/borielbasiccap02/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap02.png?fit=195%2C433&amp;ssl=1" data-orig-size="195,433" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 2" data-image-description="" data-image-caption="&lt;p&gt;Capítulo 1&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap02.png?fit=195%2C433&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap02.png?resize=195%2C433&#038;ssl=1" alt="Boriel Basic - Capítulo 2" class="wp-image-5417" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap02.png?w=195&amp;ssl=1 195w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap02.png?resize=135%2C300&amp;ssl=1 135w" sizes="auto, (max-width: 195px) 100vw, 195px" /><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 2</figcaption></figure>



<h3 id="3-el-compilador-boriel-basic" class="wp-block-heading has-large-font-size">3. El compilador Boriel Basic</h3>



<p class="has-medium-font-size wp-block-paragraph">Entramos en materia.</p>



<p class="has-medium-font-size wp-block-paragraph">Empezamos a conocer más a fondo el compilador de Boriel Basic, compilamos nuestros primeros programas desde la línea de comandos, vemos los posibles parámetros de la misma, el mapa de memoria, alguna forma de como optimizar nuestro código&nbsp; y algún concepto más que nos será de gran utilidad más adelante.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="182" height="330" data-attachment-id="5418" data-permalink="https://espamatica.com/borielbasiccap03/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap03.png?fit=182%2C330&amp;ssl=1" data-orig-size="182,330" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 3" data-image-description="" data-image-caption="&lt;p&gt;Capítulo 3&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap03.png?fit=182%2C330&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap03.png?resize=182%2C330&#038;ssl=1" alt="Boriel Basic - Capítulo 3" class="wp-image-5418" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap03.png?w=182&amp;ssl=1 182w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap03.png?resize=165%2C300&amp;ssl=1 165w" sizes="auto, (max-width: 182px) 100vw, 182px" /><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 3</figcaption></figure>



<h3 id="4-variables-y-tipos" class="wp-block-heading has-large-font-size">4. Variables y tipos</h3>



<p class="has-medium-font-size wp-block-paragraph">Es fundamental, en el aprendizaje de un lenguaje de programación, conocer de qué tipo de variables disponemos.</p>



<p class="has-medium-font-size wp-block-paragraph">En este capítulo se nos muestran las reglas a tener en cuenta a la hora de poner nombre a las variables, los tipos numéricos y las variables alfanuméricas, así como las matrices (arrays) de una o más dimensiones y como trabajar con ellas. El uso de tipos en las variables puede sonar trivial, pero es una de las claves para que nuestros programas sean más rápidos y ocupen menos memoria.</p>



<p class="has-medium-font-size wp-block-paragraph">Empezamos a prepararnos para lo que viene luego.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="242" height="332" data-attachment-id="5419" data-permalink="https://espamatica.com/borielbasiccap04/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap04.png?fit=242%2C332&amp;ssl=1" data-orig-size="242,332" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 4" data-image-description="" data-image-caption="&lt;p&gt;Capítulo 4&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap04.png?fit=242%2C332&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap04.png?resize=242%2C332&#038;ssl=1" alt="Boriel Basic - Capítulo 4" class="wp-image-5419" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap04.png?w=242&amp;ssl=1 242w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap04.png?resize=219%2C300&amp;ssl=1 219w" sizes="auto, (max-width: 242px) 100vw, 242px" /><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 4</figcaption></figure>



<h3 id="5-el-lenguaje-boriel-basic" class="wp-block-heading has-large-font-size">5. El lenguaje Boriel Basic</h3>



<p class="has-medium-font-size wp-block-paragraph">Como ya he comentado, Boriel Basic se basa en el Basic de Sinclair, pero lo redefine y extiende, por lo que tendremos instrucciones del Basic de Sinclair optimizadas para que funcionen mejor y más rápido, y otras que no existen en el Basic de Sinclair y que nos hacen la vida mucho más fácil, como DO…LOOP o ELSEIF, ELSE y END IF. Seguro que estas últimas las has echado mucho de menos si has programado en Basic de Sinclair.</p>



<p class="has-medium-font-size wp-block-paragraph">En Boriel Basic no son necesarios los números de líneas, podemos programar funciones y subrutinas, tal y como hacemos en cualquier lenguaje de alto nivel que podamos usar en la actualidad. Disponemos de librerías que nos permiten usar la rueda sin necesidad de inventarla; no necesitamos saber de qué está compuesta, sólo debemos saber para qué sirve y la forma de usarla.</p>



<p class="has-medium-font-size wp-block-paragraph">Una de las cosas que diferencian a Boriel Basic de otros Basic, y que es lo que más me llama la atención, es la posibilidad de incluir código ensamblador «inline», como si de C se tratara. ¿Una pasada, verdad?</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="325" height="369" data-attachment-id="5420" data-permalink="https://espamatica.com/borielbasiccap05/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap05.png?fit=325%2C369&amp;ssl=1" data-orig-size="325,369" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 5" data-image-description="" data-image-caption="&lt;p&gt;Capítulo 5&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap05.png?fit=325%2C369&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap05.png?resize=325%2C369&#038;ssl=1" alt="Boriel Basic - Capítulo 5" class="wp-image-5420" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap05.png?w=325&amp;ssl=1 325w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap05.png?resize=264%2C300&amp;ssl=1 264w" sizes="auto, (max-width: 325px) 100vw, 325px" /><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 5</figcaption></figure>



<h3 id="6-la-memoria" class="wp-block-heading has-large-font-size">6. La memoria</h3>



<p class="has-medium-font-size wp-block-paragraph">Pese a ser un lenguaje de alto nivel, dado el tipo de plataforma con el que estamos trabajando, se hace imposible abstraerse del hardware, al contrario de los que nos pasa con lenguajes actuales como JAVA o C#.</p>



<p class="has-medium-font-size wp-block-paragraph">Cierto conocimiento del hardware, y en este caso concreto de la memoria, es fundamental para que nuestros desarrollos lleguen a buen puerto y funcionen con unos tiempos de ejecución aceptables.</p>



<p class="has-medium-font-size wp-block-paragraph">En este capítulo se nos describe la memoria de cada uno de los modelos de ZX Spectrum, como podemos conmutar los bancos de memoria en los modelos de 128K y cómo hacerlo en el ZX Spectrum Next. Sí, ya dije que este libro también va a enseñar aspectos necesarios para programar para el ZX Spectrum Next.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="255" height="301" data-attachment-id="5421" data-permalink="https://espamatica.com/borielbasiccap06/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap06.png?fit=255%2C301&amp;ssl=1" data-orig-size="255,301" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 6" data-image-description="" data-image-caption="&lt;p&gt;Capítulo 6&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap06.png?fit=255%2C301&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap06.png?resize=255%2C301&#038;ssl=1" alt="Boriel Basic - Capítulo 6" class="wp-image-5421"/><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 6</figcaption></figure>



<h3 id="7-la-pantalla-en-el-zx-spectrum" class="wp-block-heading has-large-font-size">7. La pantalla en el ZX Spectrum</h3>



<p class="has-medium-font-size wp-block-paragraph">Gráficos, gráficos, gráficos.</p>



<p class="has-medium-font-size wp-block-paragraph">Lo que más nos gusta es lo que vemos en pantalla. Este capítulo nos enseña cómo trabajar con la pantalla del ZX Spectrum, definir UDG, fuentes y sprites y como dibujarlos, tiles y mapas y el sueño que todos hemos tenido, cuando con muy poquitos años empezamos en esto: scroll, scroll al pixel y al carácter. Todo esto lo conseguiremos usando la rueda, sin necesidad de inventarla.</p>



<p class="has-medium-font-size wp-block-paragraph">Este capítulo es uno de los más jugosos, en él empezamos a ver resultados asombrosos que nos van a dar una idea más clara de lo que podremos lograr; sólo nuestra imaginación es el límite, y los 48K o 128K de los que disponemos, no nos olvidemos.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="255" height="395" data-attachment-id="5422" data-permalink="https://espamatica.com/borielbasiccap07/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap07.png?fit=255%2C395&amp;ssl=1" data-orig-size="255,395" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 7" data-image-description="" data-image-caption="&lt;p&gt;Capítulo 7&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap07.png?fit=255%2C395&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap07.png?resize=255%2C395&#038;ssl=1" alt="Boriel Basic - Capítulo 7" class="wp-image-5422" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap07.png?w=255&amp;ssl=1 255w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap07.png?resize=194%2C300&amp;ssl=1 194w" sizes="auto, (max-width: 255px) 100vw, 255px" /><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 7</figcaption></figure>



<h3 id="8-la-pantalla-en-el-next-ngo" class="wp-block-heading has-large-font-size">8. La pantalla en el Next / N-GO</h3>



<p class="has-medium-font-size wp-block-paragraph">Siempre hemos tenido la limitación de ocho colores y hemos debido luchar contra el Color Clash, buscando la manera en la que menos se note. Bien, con el ZX Spectrum Next nos podemos olvidar de todo esto. Usa un buen número de colores distintos y olvídate del Color Clash.</p>



<p class="has-medium-font-size wp-block-paragraph">En este capítulo, al igual que en el anterior, se nos enseña cómo trabajar con sprites, tiles, mapas y scroll, pero con el hermano mayor de la familia.</p>



<p class="has-medium-font-size wp-block-paragraph">Consigue hacer todo lo que soñabas cuando eras pequeño.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="207" height="267" data-attachment-id="5423" data-permalink="https://espamatica.com/borielbasiccap08/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap08.png?fit=207%2C267&amp;ssl=1" data-orig-size="207,267" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 8" data-image-description="" data-image-caption="&lt;p&gt;Capítulo 8&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap08.png?fit=207%2C267&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap08.png?resize=207%2C267&#038;ssl=1" alt="Boriel Basic - Capítulo 8" class="wp-image-5423"/><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 8</figcaption></figure>



<h3 id="9-colisiones" class="wp-block-heading has-large-font-size">9. Colisiones</h3>



<p class="has-medium-font-size wp-block-paragraph">Seamos sinceros. Muy pocos de nosotros soñábamos con hacer un programa que nos ayudara a supervisar los gastos del hogar, ¿verdad? Soñábamos con hacer juegos como los que jugábamos, salir en la MicroHobby o Micromanía, y comprarnos ese cochazo que prometía cierta empresa si programabas para ella.</p>



<p class="has-medium-font-size wp-block-paragraph">En casi todo juego hay un protagonista, unos obstáculos y algún enemigo, y para saber por dónde puede o no pasar el protagonista, o cuando nos mata o matamos algún enemigo, tenemos que controlar lo que denominamos colisiones. En este capítulo se nos muestran distintas técnicas para ello, empieza a pensar en serio en tu próximo juego.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="206" height="297" data-attachment-id="5424" data-permalink="https://espamatica.com/borielbasiccap09/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap09.png?fit=206%2C297&amp;ssl=1" data-orig-size="206,297" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 9" data-image-description="" data-image-caption="&lt;p&gt;Capítulo 9&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap09.png?fit=206%2C297&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap09.png?resize=206%2C297&#038;ssl=1" alt="Boriel Basic - Capítulo 9" class="wp-image-5424"/><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 9</figcaption></figure>



<h3 id="10-controlando-el-spectrum" class="wp-block-heading has-large-font-size">10. Controlando el Spectrum</h3>



<p class="has-medium-font-size wp-block-paragraph">¿Qué necesita todo juego? Un jugador. ¿Qué necesita el jugador para jugar? Un teclado, un joystick o cualquier otro dispositivo que le permita interaccionar con el ZX Spectrum y, en el caso de los juegos, controlar al personaje y alcanzar la meta con él. Además también veremos como controlar un ratón en el ZX Spectrum Next.</p>



<p class="has-medium-font-size wp-block-paragraph">En este capítulo aprendemos cómo controlar cada uno de estos dispositivos para que la experiencia del usuario sea satisfactoria.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="166" height="308" data-attachment-id="5425" data-permalink="https://espamatica.com/borielbasiccap10/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap10.png?fit=166%2C308&amp;ssl=1" data-orig-size="166,308" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 10" data-image-description="" data-image-caption="&lt;p&gt;Capítulo 10&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap10.png?fit=166%2C308&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap10.png?resize=166%2C308&#038;ssl=1" alt="Boriel Basic - Capítulo 10" class="wp-image-5425" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap10.png?w=166&amp;ssl=1 166w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap10.png?resize=162%2C300&amp;ssl=1 162w" sizes="auto, (max-width: 166px) 100vw, 166px" /><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 10</figcaption></figure>



<h3 id="11-las-interrupciones" class="wp-block-heading has-large-font-size">11. Las interrupciones</h3>



<p class="has-medium-font-size wp-block-paragraph">Las interrupciones nos permiten ejecutar rutinas cada X tiempo (50 veces por segundo en sistemas PAL, 60 en sistemas NTSC), y de esta forma temporizar el movimiento de los enemigos, controlar el tiempo que queda para poder pasar un nivel, ralentizar el movimiento del personaje, etc. Este tipo de comportamiento lo hemos visto en múltiples juegos comerciales de la época. ¿Os acordáis de Manic Miner y su música ingame?</p>



<p class="has-medium-font-size wp-block-paragraph">El contenido de este capítulo es para usuarios avanzados, ya que se hará uso de ensamblador para la implementación necesaria. Además, nos muestra los entresijos de la librería IM2, desarrollada por el autor del libro, que ha sido añadida a Boriel Basic recientemente, y que nos permite gestionar las interrupciones de forma muy sencilla. Nos lo podemos saltar y volver más adelante, cuando estemos más preparados.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="199" height="353" data-attachment-id="5426" data-permalink="https://espamatica.com/borielbasiccap11/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap11.png?fit=199%2C353&amp;ssl=1" data-orig-size="199,353" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 11" data-image-description="" data-image-caption="&lt;p&gt;Boriel Basic &amp;#8211; Capítulo 11&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap11.png?fit=199%2C353&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap11.png?resize=199%2C353&#038;ssl=1" alt="Boriel Basic - Capítulo 11" class="wp-image-5426" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap11.png?w=199&amp;ssl=1 199w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap11.png?resize=169%2C300&amp;ssl=1 169w" sizes="auto, (max-width: 199px) 100vw, 199px" /><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 11</figcaption></figure>



<h3 id="12-el-beeper" class="wp-block-heading has-large-font-size">12. El Beeper</h3>



<p class="has-medium-font-size wp-block-paragraph">¿Os acordáis de él, de la instrucción BEEP, de su característico sonido?</p>



<p class="has-medium-font-size wp-block-paragraph">En este capítulo veremos maneras de llevarlo al próximo nivel, consiguiendo resultados que nunca creímos posibles, eso sí, dentro de las limitaciones que tiene.</p>



<p class="has-medium-font-size wp-block-paragraph">Léelo detenidamente y da rienda suelta a tu creatividad.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="189" height="257" data-attachment-id="5427" data-permalink="https://espamatica.com/borielbasiccap12/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap12.png?fit=189%2C257&amp;ssl=1" data-orig-size="189,257" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 12" data-image-description="" data-image-caption="&lt;p&gt;Boriel Basic &amp;#8211; Capítulo 12&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap12.png?fit=189%2C257&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap12.png?resize=189%2C257&#038;ssl=1" alt="Boriel Basic - Capítulo 12" class="wp-image-5427"/><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 12</figcaption></figure>



<h3 id="13-el-chip-ay38912" class="wp-block-heading has-large-font-size">13. El chip AY-3-8912</h3>



<p class="has-medium-font-size wp-block-paragraph">Los que pudimos disfrutar de un Amstrad CPC, pudimos disfrutar de música en multitud de juegos que en la versiones de ZX Spectrum no la tenían, caso de Commando o Ikari Warriors, por poner solo algún ejemplo.</p>



<p class="has-medium-font-size wp-block-paragraph">Por desgracia, este chip solo está presente en los modelos de 128K si hablamos de ZX Spectrum clásicos, y no fueron muchos los juegos que aprovecharon los 128K de estos modelos y el chip AY, siendo mayoría los juegos adaptados para que funcionaran en 48K, en parte debido al gran parque existente de estos equipos.</p>



<p class="has-medium-font-size wp-block-paragraph">Afortunadamente, hoy en día el homebrew existente sí que aprovecha estas características, y si bien se siguen haciendo juegos para 48K, la mayoría de las veces tienen su versión correspondiente para 128K, aunque solo sea para poder meter música, dicho sea de paso, de gran calidad en la mayoría de la ocasiones.</p>



<p class="has-medium-font-size wp-block-paragraph">En este capítulo aprenderemos como usar el chip AY para que nuestros juegos dispongan de música y sonido de calidad, y no solo en el menú, cosa habitual en el pasado.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="205" height="330" data-attachment-id="5428" data-permalink="https://espamatica.com/borielbasiccap13/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap13.png?fit=205%2C330&amp;ssl=1" data-orig-size="205,330" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 13" data-image-description="" data-image-caption="&lt;p&gt;Boriel Basic &amp;#8211; Capítulo 13&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap13.png?fit=205%2C330&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap13.png?resize=205%2C330&#038;ssl=1" alt="Boriel Basic - Capítulo 13" class="wp-image-5428" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap13.png?w=205&amp;ssl=1 205w, https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap13.png?resize=186%2C300&amp;ssl=1 186w" sizes="auto, (max-width: 205px) 100vw, 205px" /><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 13</figcaption></figure>



<h3 id="14-arquitectura" class="wp-block-heading has-large-font-size">14. Arquitectura</h3>



<p class="has-medium-font-size wp-block-paragraph">Capítulo muy importante donde se nos muestra la arquitectura que debe tener nuestro juego, como organizarlo, módulos que debe tener, etc. Para leer con mucha atención y volver a él cuando sea necesario.</p>



<p class="has-medium-font-size wp-block-paragraph">En este capítulo se propone una estructura para poder aprovechar los 128K de nuestro Spectrum +128/2/3, o los hasta 2Mb de nuestro ZX Spectrum Next.</p>



<p class="has-medium-font-size wp-block-paragraph">También nos mostrará como cargar desde la SD del ZX Spectrum Next.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="230" height="278" data-attachment-id="5429" data-permalink="https://espamatica.com/borielbasiccap14/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap14.png?fit=230%2C278&amp;ssl=1" data-orig-size="230,278" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 14" data-image-description="" data-image-caption="&lt;p&gt;Boriel Basic &amp;#8211; Capítulo 14&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap14.png?fit=230%2C278&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap14.png?resize=230%2C278&#038;ssl=1" alt="Boriel Basic - Capítulo 14" class="wp-image-5429"/><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 14</figcaption></figure>



<h3 id="15-creando-el-producto-final" class="wp-block-heading has-large-font-size">15. Creando el producto final</h3>



<p class="has-medium-font-size wp-block-paragraph">Ya tenemos nuestro juego, ¿y ahora qué? Hemos hecho un juego y lo lógico es distribuirlo para que los usuarios puedan disfrutarlo, ¿cómo lo hago?</p>



<p class="has-medium-font-size wp-block-paragraph">En este capítulo veremos cómo crear los archivos de cinta (.tap), de disco (.dsk) o para la SD del ZX Spectrum Next.</p>



<p class="has-medium-font-size wp-block-paragraph">Por último nos muestra varias opciones para su distribución, ya sea edición online o edición física. ¿Te imaginas tu juego grabado en una cinta, como en los años 80, y en la estantería de los jugones? En este capítulo verás cómo hacerlo.</p>



<p class="has-medium-font-size wp-block-paragraph">Estaría bien tener una edición física de nuestro juego tan cuidada como las de <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Play On Retro</a>.</p>



<figure class="wp-block-image size-full"><img data-recalc-dims="1" loading="lazy" decoding="async" width="201" height="262" data-attachment-id="5430" data-permalink="https://espamatica.com/borielbasiccap15/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap15.png?fit=201%2C262&amp;ssl=1" data-orig-size="201,262" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Boriel Basic &amp;#8211; Capítulo 15" data-image-description="" data-image-caption="&lt;p&gt;Boriel Basic &amp;#8211; Capítulo 15&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap15.png?fit=201%2C262&amp;ssl=1" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2024/02/BorielBasicCap15.png?resize=201%2C262&#038;ssl=1" alt="Boriel Basic - Capítulo 15" class="wp-image-5430"/><figcaption class="wp-element-caption">Boriel Basic &#8211; Capítulo 15</figcaption></figure>



<h2 id="anexos" class="wp-block-heading has-large-font-size">Anexos</h2>



<p class="has-medium-font-size wp-block-paragraph">El libro dispone de cinco anexos que enumero a continuación:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size"><strong>Anexo A</strong>: librerías incluidas en Boriel Basic.</li>



<li class="has-medium-font-size"><strong>Anexo B</strong>: directivas predefinidas.</li>



<li class="has-medium-font-size"><strong>Anexo C</strong>: códigos de las teclas.</li>



<li class="has-medium-font-size"><strong>Anexo D</strong>: decimal, binario y hexadecimal.</li>



<li class="has-medium-font-size"><strong>Anexo E</strong>: operaciones binarias.</li>
</ul>



<h2 id="recursos" class="wp-block-heading has-large-font-size">Recursos</h2>



<p class="has-medium-font-size wp-block-paragraph">Finalmente, se muestra una lista de recursos ya vistos a lo largo del libro, pero agrupados en una sola sección, que nos harán la vida más fácil en nuestro viaje a través de la programación en Boriel Basic.</p>



<h2 id="como-obtengo-el-libro" class="wp-block-heading has-large-font-size">¿Cómo obtengo el libro?</h2>



<p class="has-medium-font-size wp-block-paragraph">El libro está a la venta en Amazon, por lo que se puede obtener desde distintas partes del planeta. Consta de tres ediciones: Kindle, tapa blanda y dura. Aunque el número de páginas varía entre una edición y otra, debido al formato y tamaño de página, el contenido es exactamente el mismo, por lo que compra la que más se ajuste a tus gustos. Actualmente se está trabajando en la edición en lengua inglesa.</p>



<p class="has-medium-font-size wp-block-paragraph">A continuación, el enlace de compra. ¡Hazte con tu ejemplar!</p>



<figure class="wp-block-embed is-type-rich is-provider-amazon wp-block-embed-amazon"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Boriel Basic para ZX Spectrum: Manual para torpes...y para los que no lo son tanto" type="text/html" width="1140" height="550" frameborder="0" allowfullscreen style="max-width:100%" src="https://leer.amazon.es/kp/card?preview=inline&#038;linkCode=kpd&#038;ref_=k4w_oembed_HWsFfY29UThHWc&#038;asin=B0CQ8V5J9F&#038;tag=kpembed-20"></iframe>
</div><figcaption class="wp-element-caption">Boriel Basic en Amazon</figcaption></figure>



<h2 id="el-autor" class="wp-block-heading has-large-font-size">El autor</h2>



<p class="has-medium-font-size wp-block-paragraph">Puedes seguir a Juan Segura Durán (aka Duefectu) a través de sus redes sociales, y así mantenerte al día de todos sus proyectos.</p>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://twitter.com/Duefectu" target="_blank" rel="noreferrer noopener nofollow">X / Twiter</a></li>



<li class="has-medium-font-size"><a href="https://www.facebook.com/duefectu.corp" target="_blank" rel="noreferrer noopener nofollow">Facebook</a></li>



<li class="has-medium-font-size"><a href="https://t.me/+dSbWL8z8ol1lMjA0" target="_blank" rel="noreferrer noopener nofollow">Telegram</a></li>



<li class="has-medium-font-size"><a href="https://github.com/Duefectu" target="_blank" rel="noreferrer noopener nofollow">GitHub</a></li>



<li class="has-medium-font-size"><a href="https://www.patreon.com/DuefectuCorp" target="_blank" rel="noreferrer noopener nofollow">Patreon</a></li>



<li class="has-medium-font-size"><a href="http://www.duefectucorp.com" target="_blank" rel="noreferrer noopener nofollow">Web oficial DuefectuCorp</a></li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">Si quieres ver lo se puede llegar a desarrollar con Boriel Basic, y más concretamente para ZX Spectrum Next, visita la web del proyecto <a href="https://cuadragonnext.duefectucorp.com/" target="_blank" rel="noreferrer noopener nofollow">Cuadragon The Next Generation</a>.</p>



<h2 id="conclusion" class="wp-block-heading has-large-font-size">Conclusión</h2>



<p class="has-medium-font-size wp-block-paragraph">Ya sabéis, el límite lo pones tú. Hazte con el libro, aprende y da rienda suelta a tu creatividad.</p>



<p class="has-medium-font-size wp-block-paragraph">¡Disfrútalo!</p>



<p class="has-medium-font-size wp-block-paragraph">P.D.: quizá también te interese el libro que un tal Juanan ha escrito sobre <a href="https://espamatica.com/ensamblador-para-zx-spectrum-hacemos-un-juego/">Ensamblador para ZX Spectrum</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/boriel-basic-para-zx-spectrum/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5433</post-id>	</item>
		<item>
		<title>ZX Spectrum screen</title>
		<link>https://espamatica.com/zx-spectrum-screen/</link>
					<comments>https://espamatica.com/zx-spectrum-screen/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Wed, 13 Dec 2023 07:19:19 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=5319</guid>

					<description><![CDATA[Screen layout In this chapter we are going to talk about the screen layout of the ZX Spectrum. ZX Spectrum]]></description>
										<content:encoded><![CDATA[
<h2 id="screen-layout" class="wp-block-heading has-large-font-size">Screen layout</h2>



<p class="has-medium-font-size wp-block-paragraph">In this chapter we are going to talk about the screen layout of the ZX Spectrum.</p>



<p class="wp-block-paragraph" style="font-size:16px">Translated with <a href="https://www.deepl.com/es/translator/" target="_blank" rel="noreferrer noopener nofollow">DeepL</a>.</p>


<h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#screen-layout">Screen layout</a>
</li>
<li><a href="#zx-spectrum-graphic-area">ZX Spectrum graphic area</a>
</li>
<li><a href="#zx-spectrum-attributes-area">ZX Spectrum attributes area</a>
</li>
<li><a href="#routines">Routines</a>
</li>
<li><a href="#memory-address-from-character-coordinates">Memory address from character coordinates</a>
</li>
<li><a href="#character-coordinate-from-memory-address">Character coordinate from memory address</a>
</li>
<li><a href="#next-row-memory-address">Next row memory address</a>
</li>
<li><a href="#previous-row-memory-address">Previous row memory address</a>
</li>
<li><a href="#next-scanline">Next scanline</a>
</li>
<li><a href="#previous-scanline">Previous Scanline</a>
</li>
<li><a href="#attribute-address-from-character-coordinates">Attribute address from character coordinates</a>
</li>
<li><a href="#character-coordinates-from-attribute-address">Character coordinates from attribute address</a>
</li>
<li><a href="#attribute-address-from-graphic-area-address">Attribute address from graphic area address</a>
</li>
<li><a href="#graphic-area-address-from-attribute-address">Graphic area address from attribute address</a>
</li>
<li><a href="#conclusion">Conclusion</a>
</li></ul>


<h2 id="zx-spectrum-graphic-area" class="wp-block-heading has-large-font-size">ZX Spectrum graphic area</h2>



<p class="has-medium-font-size wp-block-paragraph">The screen of the ZX Spectrum has a resolution of 256*192 pixels, or what is the same 32 columns and 192 scanlines. With each byte we can configure 8 pixels, those at one will have the color of the ink (INK) and those at zero the color of the background (PAPER); 256 pixels / 8 pixels per byte = 32 columns. The graphic area occupies 6,144 bytes ($1800) and starts at memory address 16,384 ($4000).</p>



<p class="has-medium-font-size wp-block-paragraph">One of the peculiarities of the ZX Spectrum is the way the graphic area is distributed. Each character consists of 64 pixels, or 8&#215;8 pixels. Each line of the character is called a scanline. If we paint on the first scanline of a character and then add 32 to the memory position, the logic tells us that we are on the next scanline of the character, but it is not so because we are actually on the first scanline of the character just below the one we are on.</p>



<p class="has-medium-font-size wp-block-paragraph">If we paint position $4000, the first byte of the screen, and then we want to paint in the first scanline of row 8 of the screen, the logic says that we would only have to add 8 times 32 ($20) to position $4000, so the first scanline of the first character of row 8 would be in position $4100. But this is not the case since position $4100 corresponds to the second scanline of the first character on the screen.</p>



<p class="has-medium-font-size wp-block-paragraph">This behavior is due to the fact that the ZX Spectrum screen is divided into thirds. Each of the thirds has 8 rows and each of the rows has 8 scanlines. The first third goes from memory location $4000 to $47FF, the second third from $4800 to $4FFF and the third third third from $5000 to $57FF.</p>



<p class="has-medium-font-size wp-block-paragraph">Although this may seem strange at first, when you see the composition that the bytes take when making up a memory location, it all makes sense. The composition of the bytes is as follows:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph">010T TSSS RRRC CCCC</p>



<p class="has-medium-font-size wp-block-paragraph">Where 010 is always fixed, TT represents the third (from 0 to 2), SSS represents the scanline within the character (from 0 to 7), RRR represents the row within the third (from 0 to 7) and CCCCCC represents the column (from 0 to 31).</p>



<p class="has-medium-font-size wp-block-paragraph">If we look at the row, from 0 to 23, bits 0, 1 and 3 always indicate the row within the third, and bits 4 and 5 the third, so TTRRR is equivalent to the line in character coordinates (from 0 to 23).</p>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">Row </span><span style="color: #174781">0</span><span style="color: #002339">:  </span><span style="color: #174781">0000</span><span style="color: #002339"> </span><span style="color: #174781">0000</span><span style="color: #002339"> Row </span><span style="color: #174781">0</span><span style="color: #002339"> of third </span><span style="color: #174781">0</span></span>
<span class="line"><span style="color: #002339">Row </span><span style="color: #174781">7</span><span style="color: #002339">:  </span><span style="color: #174781">0000</span><span style="color: #002339"> </span><span style="color: #174781">0111</span><span style="color: #002339"> Row </span><span style="color: #174781">7</span><span style="color: #002339"> of third </span><span style="color: #174781">0</span></span>
<span class="line"><span style="color: #002339">Row </span><span style="color: #174781">8</span><span style="color: #002339">:  </span><span style="color: #174781">0000</span><span style="color: #002339"> </span><span style="color: #174781">1000</span><span style="color: #002339"> Row </span><span style="color: #174781">0</span><span style="color: #002339"> of third </span><span style="color: #174781">1</span></span>
<span class="line"><span style="color: #002339">Row </span><span style="color: #174781">15</span><span style="color: #002339">: </span><span style="color: #174781">0000</span><span style="color: #002339"> </span><span style="color: #174781">1111</span><span style="color: #002339"> Row </span><span style="color: #174781">7</span><span style="color: #002339"> of third </span><span style="color: #174781">1</span></span>
<span class="line"><span style="color: #002339">Row </span><span style="color: #174781">16</span><span style="color: #002339">: </span><span style="color: #174781">0001</span><span style="color: #002339"> </span><span style="color: #174781">0000</span><span style="color: #002339"> Row </span><span style="color: #174781">0</span><span style="color: #002339"> of third </span><span style="color: #174781">2</span></span>
<span class="line"><span style="color: #002339">Row </span><span style="color: #174781">23</span><span style="color: #002339">: </span><span style="color: #174781">0001</span><span style="color: #002339"> </span><span style="color: #174781">0111</span><span style="color: #002339"> Row </span><span style="color: #174781">7</span><span style="color: #002339"> of third </span><span style="color: #174781">2</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">To see all of the above, let&#8217;s perform a simple example that fills the bytes of the first column with a simple $AA pattern (1001 1001).</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org  $8000
ld   hl, $4000    ; HL = VideoRAM start address
ld   bc, $20      ; BC = value to be added to move to the next character
ld   a, $08       ; A = to draw the first scanline of the first third of the first third
loop:
ld   (hl), $aa    ; Load on screen the pattern 10011001
add  hl, bc       ; Advances HL to the next row
dec  a            ; Decrease A
jr   nz, loop     ; Loop until A reaches 0
ret               ; Back to Basic
end  $8000" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$8000</span></span>
<span class="line"><span style="color: #002339">ld   hl, </span><span style="color: #174781">$4000</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; HL = VideoRAM start address</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$20</span><span style="color: #002339">      </span><span style="color: #357B42; font-style: italic">; BC = value to be added to move to the next character</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$08</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; A = to draw the first scanline of the first third of the first third</span></span>
<span class="line"><span style="color: #7B30D0">loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld   (hl), $aa    </span><span style="color: #357B42; font-style: italic">; Load on screen the pattern 10011001</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  hl, bc       </span><span style="color: #357B42; font-style: italic">; Advances HL to the next row</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  a            </span><span style="color: #357B42; font-style: italic">; Decrease A</span></span>
<span class="line"><span style="color: #002339">jr   nz, </span><span style="color: #7B30D0">loop</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Loop until A reaches 0</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">               </span><span style="color: #357B42; font-style: italic">; Back to Basic</span></span>
<span class="line"><span style="color: #002339">end  </span><span style="color: #174781">$8000</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If we run the program, it draws the pattern $AA (10101010) in the first scanline of the first 8 rows.</p>



<p class="has-medium-font-size wp-block-paragraph">To see the ZX Spectrum screen layout, just change the LD A line, $08 and load multiples of $08; $08, $10, $18, $20, $28, $30, $38, $40, $40, $48….</p>



<p class="has-medium-font-size wp-block-paragraph">Until we reach $48, we will not see how the first scanline of the 8 rows of the second third is painted.</p>



<h2 id="zx-spectrum-attributes-area" class="wp-block-heading has-large-font-size">ZX Spectrum attributes area</h2>



<p class="has-medium-font-size wp-block-paragraph">Following the graphic area (pixels) is the attribute area, which runs from address 22,528 ($5800) to address 23,295 ($5AFF), with a length of 768 ($300) bytes.</p>



<p class="has-medium-font-size wp-block-paragraph">Each byte in the attribute area sets the attributes of a character (8&#215;8 pixels), which is the reason for the famous attribute clash.</p>



<p class="has-medium-font-size wp-block-paragraph">The format of each byte of the attribute area is as follows:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph"><strong>FBPPPIII</strong></p>



<p class="has-medium-font-size wp-block-paragraph">Where F is FLASH, B = BRIGHT, P = PAPER and I = INK.</p>



<p class="has-medium-font-size wp-block-paragraph">In the case of the attributes area the distribution is linear, so if we are in the byte of the attributes of column 0, row 0 of the screen and add 32 we are in the attributes of column 0, row 1.</p>



<p class="has-medium-font-size wp-block-paragraph">The composition of the memory addresses of the attribute area is as follows:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph"><strong>0101 10TT RRRC CCCC</strong></p>



<p class="has-medium-font-size wp-block-paragraph">Where 0101 10 is always fixed, TT represents the third (from 0 to 2), RRR represents the row within the third (from 0 to 7) and CCCCC represents the column (from 0 to 31). Again TTRRR is equivalent to the row in character coordinates (from 0 to 23).</p>



<h2 id="routines" class="wp-block-heading has-large-font-size">Routines</h2>



<p class="has-medium-font-size wp-block-paragraph">From here, we are going to see routines that can help you when working with the ZX Spectrum screen. Keep in mind that these routines do not control if the coordinates are outside the screen area.</p>



<h2 id="memory-address-from-character-coordinates" class="wp-block-heading has-large-font-size">Memory address from character coordinates</h2>



<p class="has-medium-font-size wp-block-paragraph">The CharCoord2PointerHR routine takes character coordinates and returns the corresponding memory address.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; CharCoord2PointerHR: Gets the memory address
; corresponding to the specified character coordinates. 
; 
; Entrada: B -&gt; Y-coordinate (0 a 23).
;          C -&gt; X-coordinate (0 a 31).
; 
; Salida: HL -&gt; Memory address corresponding to the
;               coordinates (010T TSSS RRRC CCCC). 
;
; Alters the value of AF and HL registers.
; ----------------------------------------------------------------- 
CharCoord2PointerHR:
ld	a, b      ; A = row. Bits 0, 1 and 2 row. Bits 3 and 4 third.
and $18       ; It stays with the third, 0001 1000. 
              ; Scanline always 0.
or 	$40       ; Add fixed part, 0100 0000.
ld 	h, a      ; H = A. High part of calculated direction.
ld 	a, b      ; A = row.
and $07       ; It stays with the row inside the third.
rrca
rrca
rrca          ; Rotate three times to pass value to bits 5, 6 and 7.Rotate three times to pass value to bits 5, 6 and 7.
or 	c         ; Add column.
ld 	l, a      ; L = A. Lower part of calculated direction.
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; CharCoord2PointerHR: Gets the memory address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; corresponding to the specified character coordinates. </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: B -&gt; Y-coordinate (0 a 23).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;          C -&gt; X-coordinate (0 a 31).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; Memory address corresponding to the</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               coordinates (010T TSSS RRRC CCCC). </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">CharCoord2PointerHR:</span></span>
<span class="line"><span style="color: #002339">ld	a, b      </span><span style="color: #357B42; font-style: italic">; A = row. Bits 0, 1 and 2 row. Bits 3 and 4 third.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$18</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; It stays with the third, 0001 1000. </span></span>
<span class="line"><span style="color: #002339">              </span><span style="color: #357B42; font-style: italic">; Scanline always 0.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> 	</span><span style="color: #174781">$40</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; Add fixed part, 0100 0000.</span></span>
<span class="line"><span style="color: #002339">ld 	h, a      </span><span style="color: #357B42; font-style: italic">; H = A. High part of calculated direction.</span></span>
<span class="line"><span style="color: #002339">ld 	a, b      </span><span style="color: #357B42; font-style: italic">; A = row.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$07</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; It stays with the row inside the third.</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca          </span><span style="color: #357B42; font-style: italic">; Rotate three times to pass value to bits 5, 6 and 7.Rotate three times to pass value to bits 5, 6 and 7.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> 	c         </span><span style="color: #357B42; font-style: italic">; Add column.</span></span>
<span class="line"><span style="color: #002339">ld 	l, a      </span><span style="color: #357B42; font-style: italic">; L = A. Lower part of calculated direction.</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">To test this routine we can pass it positions and then load a pattern at the returned address to see where it draws.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org $8000

ld b, $10                     ; Load in B the row.
ld c, $10                     ; Load in C the column.
call CharCoord2PointerHR      ; Calculates memory location.
ld (hl), $ff                  ; Activates all pixels in the
                              ; memory location.

inc b                         ; Increases row.
inc c                         ; Increases column.
call CharCoord2PointerHR      ; Calculates memory location.
ld (hl), $ff                  ; Activates all pixels in the
                              ; memory location.

inc b                         ; Increases row.
dec c                         ; Increases column.
call CharCoord2PointerHR      ; Calculates memory location.
ld (hl), $ff                  ; Activates all pixels in the
                              ; memory location.

ret

end	 $8000" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org </span><span style="color: #174781">$8000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld b, </span><span style="color: #174781">$10</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Load in B the row.</span></span>
<span class="line"><span style="color: #002339">ld c, </span><span style="color: #174781">$10</span><span style="color: #002339">                     </span><span style="color: #357B42; font-style: italic">; Load in C the column.</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CharCoord2PointerHR      </span><span style="color: #357B42; font-style: italic">; Calculates memory location.</span></span>
<span class="line"><span style="color: #002339">ld (hl), $ff                  </span><span style="color: #357B42; font-style: italic">; Activates all pixels in the</span></span>
<span class="line"><span style="color: #002339">                              </span><span style="color: #357B42; font-style: italic">; memory location.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339"> b                         </span><span style="color: #357B42; font-style: italic">; Increases row.</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339"> c                         </span><span style="color: #357B42; font-style: italic">; Increases column.</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CharCoord2PointerHR      </span><span style="color: #357B42; font-style: italic">; Calculates memory location.</span></span>
<span class="line"><span style="color: #002339">ld (hl), $ff                  </span><span style="color: #357B42; font-style: italic">; Activates all pixels in the</span></span>
<span class="line"><span style="color: #002339">                              </span><span style="color: #357B42; font-style: italic">; memory location.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339"> b                         </span><span style="color: #357B42; font-style: italic">; Increases row.</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339"> c                         </span><span style="color: #357B42; font-style: italic">; Increases column.</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CharCoord2PointerHR      </span><span style="color: #357B42; font-style: italic">; Calculates memory location.</span></span>
<span class="line"><span style="color: #002339">ld (hl), $ff                  </span><span style="color: #357B42; font-style: italic">; Activates all pixels in the</span></span>
<span class="line"><span style="color: #002339">                              </span><span style="color: #357B42; font-style: italic">; memory location.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">end	 </span><span style="color: #174781">$8000</span></span></code></pre></div>



<h2 id="character-coordinate-from-memory-address" class="wp-block-heading has-large-font-size">Character coordinate from memory address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHR2CharCoord routine takes a memory address and returns the corresponding character coordinates.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ---------------------------------------------------------------- 
; PointerHR2CharCoord: Obtiene las coordenadas de carácter
; correspondientes a la dirección de memoria especificada.
;
; Entrada: HL -&gt; Memory pointer. 010T TSSS RRRC CCCC.
;
; Salida: B -&gt; Y-coordinate (0 a 23).
;         C -&gt; X-coordinate (0 a 31).
;
; Alters the value of AF and BC registers.  
; ---------------------------------------------------------------- 
PointerHR2CharCoord: 
ld a, h     ; A = upper part of memory address. 010T TSSS.
and $18     ; Keeps the third. Bits 4 and 5 of the Y coordinate.
            ; Scanline always at 0, they are character coordinates.
ld b, a     ; B = A.

ld a, l     ; A = lower part of the memory address. RRRC CCCC.
and $e0     ; It stays with the row inside the third.
rrca
rrca
rrca        ; Rotate three times to pass value to bits 0, 1 and 3.
or b        ; Add bits 4 and 5.
ld b, a     ; B = A. Calculated Y-coordinate.

ld a, l     ; A = lower part of the memory address. RRRC CCCC.
and $1f     ; It keeps the column.
ld c, a     ; C = A. Calculated X-coordinate.

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHR2CharCoord: Obtiene las coordenadas de carácter</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; correspondientes a la dirección de memoria especificada.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Memory pointer. 010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: B -&gt; Y-coordinate (0 a 23).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;         C -&gt; X-coordinate (0 a 31).</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and BC registers.  </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHR2CharCoord:</span><span style="color: #002339"> </span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of memory address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$18</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the third. Bits 4 and 5 of the Y coordinate.</span></span>
<span class="line"><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Scanline always at 0, they are character coordinates.</span></span>
<span class="line"><span style="color: #002339">ld b, a     </span><span style="color: #357B42; font-style: italic">; B = A.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the memory address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> $e0     </span><span style="color: #357B42; font-style: italic">; It stays with the row inside the third.</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca        </span><span style="color: #357B42; font-style: italic">; Rotate three times to pass value to bits 0, 1 and 3.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> b        </span><span style="color: #357B42; font-style: italic">; Add bits 4 and 5.</span></span>
<span class="line"><span style="color: #002339">ld b, a     </span><span style="color: #357B42; font-style: italic">; B = A. Calculated Y-coordinate.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the memory address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$1f</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; It keeps the column.</span></span>
<span class="line"><span style="color: #002339">ld c, a     </span><span style="color: #357B42; font-style: italic">; C = A. Calculated X-coordinate.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<h2 id="next-row-memory-address" class="wp-block-heading has-large-font-size">Next row memory address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHRNextLine routine takes a memory address and returns the memory address corresponding to the next row.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerHRNextLine: gets the memory address
; corresponding to the next row.
;
; Entrada: HL -&gt; Current address. 010T TSSS RRRC CCCC.
;
; Salida: HL -&gt; Dirección de la fila siguiente.
;               010T TSSS RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerHRNextLine:
ld a, l     ; A = lower part of the address. RRRC CCCC.
add a, $20  ; Add one line (RRRC CCCC + 0010 0000).
ld l, a     ; L = A.
ret nc      ; If there is no carry, the row follows between 0 
            ; and 7, it exits.

; There is carry-over, the third of the screen has to be changed.
ld a, h     ; A = upper part of memory address. 010T TSSS.
add a, $08  ; Add one third (010T TSSS + 0000 1000).
ld h, a     ; H = A.

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHRNextLine: gets the memory address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; corresponding to the next row.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Current address. 010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; Dirección de la fila siguiente.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHRNextLine:</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339"> a, </span><span style="color: #174781">$20</span><span style="color: #002339">  </span><span style="color: #357B42; font-style: italic">; Add one line (RRRC CCCC + 0010 0000).</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A.</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339"> nc      </span><span style="color: #357B42; font-style: italic">; If there is no carry, the row follows between 0 </span></span>
<span class="line"><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; and 7, it exits.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; There is carry-over, the third of the screen has to be changed.</span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of memory address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339"> a, </span><span style="color: #174781">$08</span><span style="color: #002339">  </span><span style="color: #357B42; font-style: italic">; Add one third (010T TSSS + 0000 1000).</span></span>
<span class="line"><span style="color: #002339">ld h, a     </span><span style="color: #357B42; font-style: italic">; H = A.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<h2 id="previous-row-memory-address" class="wp-block-heading has-large-font-size">Previous row memory address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHRPreviousLine routine takes a memory address and returns the memory address corresponding to the previous row.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerHRPreviousLine: gets the memory address
; corresponding to the previous row.
;
; Entrada: HL -&gt; Current address. 010T TSSS RRRC CCCC.
;
; Salida: HL -&gt; Dirección de la fila anterior.
;               010T TSSS RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerHRPreviousLine:
ld a, l     ; A = lower part of the address. RRRC CCCC.
and $e0     ; It keeps the row part (1110 0000).
jr z, PointerHRPreviousLine_continue   ; If line is 0,
                                       ; change of third.

; No change of third.
ld a, l     ; A = lower part of the address, RRRC CCCC.
sub $20     ; Subtract one row (RRRC CCCC - 0010 0000).
ld l, a     ; L = A. Calculated address.

ret

PointerHRPreviousLine_continue:
; It is necessary to change the third party.
ld a, l     ; A = lower part of the address. RRRC CCCC.
or $e0      ; Set the row to 7 (RRRC CCCC or 1110 0000).
ld l, a     ; L = A. Lower part of the calculated address.

ld a, h     ; A = upper part of the address. 010T TSSS.
sub $08     ; Subtract one third (010T TSSS - 0000 1000).
ld h, a     ; H = A. Upper part of the calculated address.

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHRPreviousLine: gets the memory address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; corresponding to the previous row.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Current address. 010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; Dirección de la fila anterior.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousLine:</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> $e0     </span><span style="color: #357B42; font-style: italic">; It keeps the row part (1110 0000).</span></span>
<span class="line"><span style="color: #002339">jr z, PointerHRPreviousLine_continue   </span><span style="color: #357B42; font-style: italic">; If line is 0,</span></span>
<span class="line"><span style="color: #002339">                                       </span><span style="color: #357B42; font-style: italic">; change of third.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; No change of third.</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address, RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Subtract one row (RRRC CCCC - 0010 0000).</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A. Calculated address.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousLine_continue:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; It is necessary to change the third party.</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> $e0      </span><span style="color: #357B42; font-style: italic">; Set the row to 7 (RRRC CCCC or 1110 0000).</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A. Lower part of the calculated address.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339"> </span><span style="color: #174781">$08</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Subtract one third (010T TSSS - 0000 1000).</span></span>
<span class="line"><span style="color: #002339">ld h, a     </span><span style="color: #357B42; font-style: italic">; H = A. Upper part of the calculated address.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<h2 id="next-scanline" class="wp-block-heading has-large-font-size">Next scanline</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHRNextScanLine routine takes a memory address and returns the address of the next scanline.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerHRNextScanLine: gets the memory address
; corresponding to the next scanline.
;
; Entrada: HL -&gt; current address. 010T TSSS RRRC CCCC.
;
; Salida: HL -&gt; address of the next scanline.
;               010T TSSS RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerHRNextScanLine:
ld a, h     ; A = upper part of the address. 010T TSSS.
and $07     ; Keeps the scanline.
cp $07      ; Check if scanline is 7.
jr z, PointerHRNextScanLine_continue     ; Yes, change of line.

; Scanline is not 7.
inc h       ; Increases the scanline by 1 and exits.

ret

PointerHRNextScanLine_continue:
; The row must be changed.
ld a, l     ; A = lower part of the address. RRRC CCCC.
add a, $20  ; Add one line (RRRC CCCC + 0010 0000).
ld l, a     ; L = A.
ld a, h     ; A = upper part of the address. 010T TSSS.
jr nc, PointerHRNextScanLine_end  ; If there is no carriage, skip
                                  ; to finish the calculation.

; There is carriage, it is necessary to change the third party.
add a, $08  ; Add one to the third (010T TSSS + 0000 1000).

PointerHRNextScanLine_end:
and $f8     ; Keeps the fixed part and the third part.
            ; Set the scanline to 0.
ld h, a     ; H = A. Calculated address.

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHRNextScanLine: gets the memory address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; corresponding to the next scanline.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; current address. 010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; address of the next scanline.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHRNextScanLine:</span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$07</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the scanline.</span></span>
<span class="line"><span style="color: #002339">cp </span><span style="color: #174781">$07</span><span style="color: #002339">      </span><span style="color: #357B42; font-style: italic">; Check if scanline is 7.</span></span>
<span class="line"><span style="color: #002339">jr z, PointerHRNextScanLine_continue     </span><span style="color: #357B42; font-style: italic">; Yes, change of line.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Scanline is not 7.</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339"> h       </span><span style="color: #357B42; font-style: italic">; Increases the scanline by 1 and exits.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRNextScanLine_continue:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; The row must be changed.</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339"> a, </span><span style="color: #174781">$20</span><span style="color: #002339">  </span><span style="color: #357B42; font-style: italic">; Add one line (RRRC CCCC + 0010 0000).</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A.</span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #002339">jr nc, PointerHRNextScanLine_end  </span><span style="color: #357B42; font-style: italic">; If there is no carriage, skip</span></span>
<span class="line"><span style="color: #002339">                                  </span><span style="color: #357B42; font-style: italic">; to finish the calculation.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; There is carriage, it is necessary to change the third party.</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339"> a, </span><span style="color: #174781">$08</span><span style="color: #002339">  </span><span style="color: #357B42; font-style: italic">; Add one to the third (010T TSSS + 0000 1000).</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRNextScanLine_end:</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> $f8     </span><span style="color: #357B42; font-style: italic">; Keeps the fixed part and the third part.</span></span>
<span class="line"><span style="color: #002339">            </span><span style="color: #357B42; font-style: italic">; Set the scanline to 0.</span></span>
<span class="line"><span style="color: #002339">ld h, a     </span><span style="color: #357B42; font-style: italic">; H = A. Calculated address.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">To test the routine, let&#8217;s use a small program to see how it draws. Contrary to what we saw in the first example, now we paint a scanline followed by the one right behind it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org $8000

ld hl, $4000    ; HL = VideoRAM start address.
ld b, $08       ; B = to draw 8 scanlines.

loop:
ld (hl), $aa    ; Loads the $AA pattern on screen.
call PointerHRNextScanLine  ; Advances to next scanline.
djnz loop       ; Loop until B reaches 0.

ret

end	 $8000" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org </span><span style="color: #174781">$8000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld hl, </span><span style="color: #174781">$4000</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; HL = VideoRAM start address.</span></span>
<span class="line"><span style="color: #002339">ld b, </span><span style="color: #174781">$08</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; B = to draw 8 scanlines.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld (hl), $aa    </span><span style="color: #357B42; font-style: italic">; Loads the $AA pattern on screen.</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PointerHRNextScanLine  </span><span style="color: #357B42; font-style: italic">; Advances to next scanline.</span></span>
<span class="line"><span style="color: #002339">djnz </span><span style="color: #7B30D0">loop</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; Loop until B reaches 0.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">end	 </span><span style="color: #174781">$8000</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If we run the program, it draws the $AA pattern on the first character. The first example did it on the first scanline of the first 8 rows.</p>



<p class="has-medium-font-size wp-block-paragraph">As we did in the first example, we can load multiples of $08 in B to see how the program is drawing; $08, $10, $18, $20, $28, $30, $38, $40, $48….</p>



<h2 id="previous-scanline" class="wp-block-heading has-large-font-size">Previous Scanline</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHRPreviousScanLine routine takes a memory address and returns the memory address corresponding to the previous scanline.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerHRPreviousScanLine: gets the memory address
; corresponding to the previous scanline.
;
; Entrada: HL -&gt; current address. 010T TSSS RRRC CCCC.
;
; Salida: HL -&gt; address of previous scanline.
;               010T TSSS RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerHRPreviousScanLine:
ld a, h     ; A = upper part of the address. 010T TSSS.
and $07     ; Keeps the scanline.
or a        ; Check if it is at 0.
jr z, PointerHRPreviousScanLine_continue  ; Yes, change line.

; No change of line.
dec h       ; Decrements the scanline and exits.

ret

PointerHRPreviousScanLine_continue:
ld a, l     ; A = lower part of the address. RRRC CCCC.
and $e0     ; Keeps the row part.
or a        ; Check if it is at 0.
jr z, PointerHRPreviousScanLine_change  ; Yes, change third.

; No need to change third.
ld a, l     ; A = lower part of the address. RRRC CCCC.
sub $20     ; Subtract one row, (RRRC CCCC - 0010 0000).
ld l, a     ; L = A.
jr PointerHRPreviousScanLine_end  ; Skip to end of calculation.

PointerHRPreviousScanLine_change:
; Need to change third.
ld a, l     ; A = lower part of the address. RRRC CCCC.
or $e0      ; Sets the line to 7.
ld l, a     ; L = A.

ld a, h     ; A = upper part of the address. 010T TSSS.
sub $08     ; Subtract one to the third.
ld h, a     ; H = A.

PointerHRPreviousScanLine_end:
ld a, h     ; A = upper part of the address. 010T TSSS.
and $F8     ; Keeps the fixed part and the third part.
or $07      ; Set the scanline to 7.
ld h, a     ; H = A.

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHRPreviousScanLine: gets the memory address</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; corresponding to the previous scanline.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; current address. 010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; address of previous scanline.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousScanLine:</span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> </span><span style="color: #174781">$07</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the scanline.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> a        </span><span style="color: #357B42; font-style: italic">; Check if it is at 0.</span></span>
<span class="line"><span style="color: #002339">jr z, PointerHRPreviousScanLine_continue  </span><span style="color: #357B42; font-style: italic">; Yes, change line.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; No change of line.</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339"> h       </span><span style="color: #357B42; font-style: italic">; Decrements the scanline and exits.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousScanLine_continue:</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> $e0     </span><span style="color: #357B42; font-style: italic">; Keeps the row part.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> a        </span><span style="color: #357B42; font-style: italic">; Check if it is at 0.</span></span>
<span class="line"><span style="color: #002339">jr z, PointerHRPreviousScanLine_change  </span><span style="color: #357B42; font-style: italic">; Yes, change third.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; No need to change third.</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Subtract one row, (RRRC CCCC - 0010 0000).</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A.</span></span>
<span class="line"><span style="color: #002339">jr PointerHRPreviousScanLine_end  </span><span style="color: #357B42; font-style: italic">; Skip to end of calculation.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousScanLine_change:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Need to change third.</span></span>
<span class="line"><span style="color: #002339">ld a, l     </span><span style="color: #357B42; font-style: italic">; A = lower part of the address. RRRC CCCC.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> $e0      </span><span style="color: #357B42; font-style: italic">; Sets the line to 7.</span></span>
<span class="line"><span style="color: #002339">ld l, a     </span><span style="color: #357B42; font-style: italic">; L = A.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339"> </span><span style="color: #174781">$08</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Subtract one to the third.</span></span>
<span class="line"><span style="color: #002339">ld h, a     </span><span style="color: #357B42; font-style: italic">; H = A.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">PointerHRPreviousScanLine_end:</span></span>
<span class="line"><span style="color: #002339">ld a, h     </span><span style="color: #357B42; font-style: italic">; A = upper part of the address. 010T TSSS.</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339"> $F8     </span><span style="color: #357B42; font-style: italic">; Keeps the fixed part and the third part.</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339"> </span><span style="color: #174781">$07</span><span style="color: #002339">      </span><span style="color: #357B42; font-style: italic">; Set the scanline to 7.</span></span>
<span class="line"><span style="color: #002339">ld h, a     </span><span style="color: #357B42; font-style: italic">; H = A.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And in order to test the routine, another simple program.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org $8000

ld hl, $57ff    ; HL = VideoRAM end address.
ld b, $08       ; B = to draw 8 scanlines.

loop:
ld (hl), $aa    ; Loads the $AA pattern on the screen.
call PointerHRPreviousScanLine  ; Back to previous scanline.
djnz loop       ; Loop until B reaches 0.

ret

end	 $8000" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org </span><span style="color: #174781">$8000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld hl, </span><span style="color: #174781">$57ff</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; HL = VideoRAM end address.</span></span>
<span class="line"><span style="color: #002339">ld b, </span><span style="color: #174781">$08</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; B = to draw 8 scanlines.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld (hl), $aa    </span><span style="color: #357B42; font-style: italic">; Loads the $AA pattern on the screen.</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PointerHRPreviousScanLine  </span><span style="color: #357B42; font-style: italic">; Back to previous scanline.</span></span>
<span class="line"><span style="color: #002339">djnz </span><span style="color: #7B30D0">loop</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; Loop until B reaches 0.</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">end	 </span><span style="color: #174781">$8000</span></span></code></pre></div>



<h2 id="attribute-address-from-character-coordinates" class="wp-block-heading has-large-font-size">Attribute address from character coordinates</h2>



<p class="has-medium-font-size wp-block-paragraph">The CharCoord2PointerAttr routine takes character coordinates and gets the corresponding attribute memory address.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; CharCoord2PointerAttr: takes character coordinates and obtains
; the memory address of the corresponding attribute.
;
; Entrada: B -&gt; Y-coordinate
;          C -&gt; X-coordinate
;
; Salida: HL -&gt; Attribute memory address.
;               0101 10TT RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
CharCoord2PointerAttr:
ld   a, b    ; Load row in A
and  $18     ; Keeps the third bits
rrca
rrca
rrca         ; Pass them to bits 0 and 1
or   $58     ; Adds the fixed bits of the high part of the address
ld   h, a    ; H = 0101 10TT
ld   a, b    ; Load row in A
and  $07     ; Keeps the bits of the row
rrca
rrca
rrca         ; Pass them to bits 5, 6, and 7
or   c       ; Adds the bits of the column
ld   l, a    ; L = RRRC CCCC

ret          ; HL = 0101 10TT RRRC CCCC (attribute address)" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; CharCoord2PointerAttr: takes character coordinates and obtains</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; the memory address of the corresponding attribute.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: B -&gt; Y-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;          C -&gt; X-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; Attribute memory address.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               0101 10TT RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">CharCoord2PointerAttr:</span></span>
<span class="line"><span style="color: #002339">ld   a, b    </span><span style="color: #357B42; font-style: italic">; Load row in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$18</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the third bits</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca         </span><span style="color: #357B42; font-style: italic">; Pass them to bits 0 and 1</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   </span><span style="color: #174781">$58</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Adds the fixed bits of the high part of the address</span></span>
<span class="line"><span style="color: #002339">ld   h, a    </span><span style="color: #357B42; font-style: italic">; H = 0101 10TT</span></span>
<span class="line"><span style="color: #002339">ld   a, b    </span><span style="color: #357B42; font-style: italic">; Load row in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$07</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the bits of the row</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca         </span><span style="color: #357B42; font-style: italic">; Pass them to bits 5, 6, and 7</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   c       </span><span style="color: #357B42; font-style: italic">; Adds the bits of the column</span></span>
<span class="line"><span style="color: #002339">ld   l, a    </span><span style="color: #357B42; font-style: italic">; L = RRRC CCCC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = 0101 10TT RRRC CCCC (attribute address)</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Social networks can be wonderful; <a href="https://twitter.com/NathanielSalis" target="_blank" rel="noreferrer noopener nofollow">NathanielSalis</a> made me see that in this routine we can save bytes and clock cycles by dispensing with three rotations. The code would look like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; CharCoord2PointerAttr: takes character coordinates and obtains
; the memory address of the corresponding attribute.
;
; Entrada: B -&gt; Y-coordinate
;          C -&gt; X-coordinate
;
; Salida: HL -&gt; Attribute memory address.
;               0101 10TT RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
CharCoord2PointerAttr:
ld   a, b    ; Load row in A
; Before keeping the bits of the third, we carry out the rotations
rrca
rrca         ; Passes the bits of the third to bits 0 and 1
rrca         ; and those of the row to bits 5, 6 and 7
ld   l, a    ; Load the result in L
and  $03     ; A = bits of the third
or   $58     ; Adds the fixed bits of the high part of the address
ld   h, a    ; H = 0101 10TT
ld   a, l    ; A = row in bits 5, 6 and 7 and third in bits 0 and 1
and  $e0     ; Keeps the bits of the line
or   c       ; Adds the bits of the column
ld   l, a    ; L = RRRC CCCC

ret          ; HL = 0101 10TT RRRC CCCC (attribute address)" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; CharCoord2PointerAttr: takes character coordinates and obtains</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; the memory address of the corresponding attribute.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: B -&gt; Y-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;          C -&gt; X-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida: HL -&gt; Attribute memory address.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;               0101 10TT RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">CharCoord2PointerAttr:</span></span>
<span class="line"><span style="color: #002339">ld   a, b    </span><span style="color: #357B42; font-style: italic">; Load row in A</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Before keeping the bits of the third, we carry out the rotations</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca         </span><span style="color: #357B42; font-style: italic">; Passes the bits of the third to bits 0 and 1</span></span>
<span class="line"><span style="color: #002339">rrca         </span><span style="color: #357B42; font-style: italic">; and those of the row to bits 5, 6 and 7</span></span>
<span class="line"><span style="color: #002339">ld   l, a    </span><span style="color: #357B42; font-style: italic">; Load the result in L</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; A = bits of the third</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   </span><span style="color: #174781">$58</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Adds the fixed bits of the high part of the address</span></span>
<span class="line"><span style="color: #002339">ld   h, a    </span><span style="color: #357B42; font-style: italic">; H = 0101 10TT</span></span>
<span class="line"><span style="color: #002339">ld   a, l    </span><span style="color: #357B42; font-style: italic">; A = row in bits 5, 6 and 7 and third in bits 0 and 1</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  $e0     </span><span style="color: #357B42; font-style: italic">; Keeps the bits of the line</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   c       </span><span style="color: #357B42; font-style: italic">; Adds the bits of the column</span></span>
<span class="line"><span style="color: #002339">ld   l, a    </span><span style="color: #357B42; font-style: italic">; L = RRRC CCCC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = 0101 10TT RRRC CCCC (attribute address)</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The first version of the routine occupies 18 bytes and takes 75 cycles to execute, while the second occupies 16 bytes and takes 67 cycles to execute, so we save two bytes and 8 cycles.</p>



<h2 id="character-coordinates-from-attribute-address" class="wp-block-heading has-large-font-size">Character coordinates from attribute address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerAttr2CharCoord routine takes a memory address of an attribute and returns the corresponding character coordinates.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerAttr2CharCoord: takes an attribute memory address 
; and returns the corresponding character coordinates.
;
; Entrada: HL -&gt; Attribute memory address.
;                0101 10TT RRRC CCCC.
;
; Salida:   B -&gt; Y-coordinate
;           C -&gt; X-coordinate
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerAttr2CharCoord:
ld   a, h    ; Loads the upper part of the address in A
and  $03     ; Keeps the bits of the third
rlca
rlca
rlca         ; Passes them to bits 3 and 4
ld   b, a    ; B = 000TT000
ld   a, l    ; Load the lower part of the address in A
and  $e0     ; Keeps the bits in the row
rlca
rlca
rlca         ; Passes them to bits 0, 1 and 2
or   b       ; Add the third
ld   b, a    ; B = TTCCC (Y-coordinate)
ld   a, l    ; Load the lower part of the address in A
and  $1f     ; It keeps the bits of the column
ld   c, a    ; C = 000CCCCC (X-coordinate)

ret          ; BC = character coordinates" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerAttr2CharCoord: takes an attribute memory address </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; and returns the corresponding character coordinates.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Attribute memory address.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                0101 10TT RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida:   B -&gt; Y-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;           C -&gt; X-coordinate</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerAttr2CharCoord:</span></span>
<span class="line"><span style="color: #002339">ld   a, h    </span><span style="color: #357B42; font-style: italic">; Loads the upper part of the address in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the bits of the third</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca         </span><span style="color: #357B42; font-style: italic">; Passes them to bits 3 and 4</span></span>
<span class="line"><span style="color: #002339">ld   b, a    </span><span style="color: #357B42; font-style: italic">; B = 000TT000</span></span>
<span class="line"><span style="color: #002339">ld   a, l    </span><span style="color: #357B42; font-style: italic">; Load the lower part of the address in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  $e0     </span><span style="color: #357B42; font-style: italic">; Keeps the bits in the row</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca         </span><span style="color: #357B42; font-style: italic">; Passes them to bits 0, 1 and 2</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   b       </span><span style="color: #357B42; font-style: italic">; Add the third</span></span>
<span class="line"><span style="color: #002339">ld   b, a    </span><span style="color: #357B42; font-style: italic">; B = TTCCC (Y-coordinate)</span></span>
<span class="line"><span style="color: #002339">ld   a, l    </span><span style="color: #357B42; font-style: italic">; Load the lower part of the address in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$1f</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; It keeps the bits of the column</span></span>
<span class="line"><span style="color: #002339">ld   c, a    </span><span style="color: #357B42; font-style: italic">; C = 000CCCCC (X-coordinate)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; BC = character coordinates</span></span></code></pre></div>



<h2 id="attribute-address-from-graphic-area-address" class="wp-block-heading has-large-font-size">Attribute address from graphic area address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerHR2PointerAttr routine takes a memory address from the graphic area and returns the corresponding attribute memory address.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerHR2PointerAttr: takes a memory address of the graphic area 
; and returns the memory address of the corresponding attribute.
;
; Entrada: HL -&gt; Memory address of the graphic area.
;                010T TSSS RRRC CCCC.
;
; Salida:  HL -&gt; Attribute memory address.
;                0101 10TT RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerHR2PointerAttr:
ld   a, h    ; Loads the upper part of the address in A
and  $18     ; Keeps the bits of the third
rrca
rrca
rrca         ; Passes them to bits 0 and 1
or   $58     ; Add the fixed part
ld   h, a    ; A = 010110TT

ret          ; HL = attribute memory address" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerHR2PointerAttr: takes a memory address of the graphic area </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; and returns the memory address of the corresponding attribute.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Memory address of the graphic area.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida:  HL -&gt; Attribute memory address.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                0101 10TT RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerHR2PointerAttr:</span></span>
<span class="line"><span style="color: #002339">ld   a, h    </span><span style="color: #357B42; font-style: italic">; Loads the upper part of the address in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$18</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the bits of the third</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca         </span><span style="color: #357B42; font-style: italic">; Passes them to bits 0 and 1</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   </span><span style="color: #174781">$58</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Add the fixed part</span></span>
<span class="line"><span style="color: #002339">ld   h, a    </span><span style="color: #357B42; font-style: italic">; A = 010110TT</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = attribute memory address</span></span></code></pre></div>



<h2 id="graphic-area-address-from-attribute-address" class="wp-block-heading has-large-font-size">Graphic area address from attribute address</h2>



<p class="has-medium-font-size wp-block-paragraph">The PointerAttr2PointerHR routine takes an attribute memory address and returns the memory address of the graphic area corresponding to scanline 0.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; ----------------------------------------------------------------
; PointerAttr2PointerHR: takes a memory address of attribute
; and returns the memory address of the corresponding graphic area
; to the first scanline.
;
; Entrada: HL -&gt; Attribute memory address.
;                0101 10TT RRRC CCCC.
;
; Salida:  HL -&gt; Memory address of the graphic area.
;                010T TSSS RRRC CCCC.
;
; Alters the value of AF and HL registers.
; ---------------------------------------------------------------- 
PointerAttr2PointerHR:
ld   a, h    ; Loads the upper part of the address in A
and  $03     ; Keeps the bits of the third
rlca
rlca
rlca         ; Passes them to bits 0 and 1
or   $40     ; Add the fixed part
ld   h, a    ; A = 010TT000

ret          ; HL = address of the first scanline" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; PointerAttr2PointerHR: takes a memory address of attribute</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; and returns the memory address of the corresponding graphic area</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; to the first scanline.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Entrada: HL -&gt; Attribute memory address.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                0101 10TT RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Salida:  HL -&gt; Memory address of the graphic area.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                010T TSSS RRRC CCCC.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ---------------------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PointerAttr2PointerHR:</span></span>
<span class="line"><span style="color: #002339">ld   a, h    </span><span style="color: #357B42; font-style: italic">; Loads the upper part of the address in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Keeps the bits of the third</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca</span></span>
<span class="line"><span style="color: #002339">rlca         </span><span style="color: #357B42; font-style: italic">; Passes them to bits 0 and 1</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   </span><span style="color: #174781">$40</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Add the fixed part</span></span>
<span class="line"><span style="color: #002339">ld   h, a    </span><span style="color: #357B42; font-style: italic">; A = 010TT000</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = address of the first scanline</span></span></code></pre></div>



<h2 id="conclusion" class="wp-block-heading has-large-font-size">Conclusion</h2>



<p class="has-medium-font-size wp-block-paragraph">Everything I have put here, I have learned by following the <a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">assembler course</a> found at speccy.org and reading the book Código Máquina del ZX-Spectrum by Jesús Alonso Cano.</p>



<p class="has-medium-font-size wp-block-paragraph">The routines shown here are the exercises I have done to practice what I have learned.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-medium-font-size wp-block-paragraph">You can also visit the rest of tutorials:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Z80
<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/bcd-numbers/">BCD numbers</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/zx-spectrum-screen">ZX Spectrum screen</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-pong-0x0ff-introduction/">ZX-Pong</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-space-battle-0x00-introduction/">Space Battle</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-tic-tac-toe-0x01-game-board/">Tic-Tac-Toe</a></li>
</ul>
</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">And remember, if you use it, don&#8217;t just copy it, try to understand it and adapt it to your needs.</p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-screen/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5319</post-id>	</item>
		<item>
		<title>Z80 Assembly &#8211; BCD numbers</title>
		<link>https://espamatica.com/bcd-numbers/</link>
					<comments>https://espamatica.com/bcd-numbers/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Tue, 17 Oct 2023 09:28:33 +0000</pubDate>
				<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Amstrad CPC]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=5017</guid>

					<description><![CDATA[In this chapter of Z80 Assembly, we will learn how to work with numbers larger than 16 bits. What are]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of Z80 Assembly, we will learn how to work with numbers larger than 16 bits.</p>



<p class="wp-block-paragraph" style="font-size:16px">Translated with <a href="https://www.deepl.com/es/translator/" target="_blank" rel="noreferrer noopener nofollow">DeepL</a>.</p>


<h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#what-are-bcd-numbers">What are BCD numbers?</a>
</li>
<li><a href="#how-to-use-them">How to use them?</a>
</li>
<li><a href="#how-do-you-paint-them">How do you paint them?</a>
</li>
<li><a href="#conclusion">Conclusion</a>
</li></ul>


<h2 id="what-are-bcd-numbers" class="wp-block-heading has-large-font-size">What are BCD numbers?</h2>



<p class="has-medium-font-size wp-block-paragraph"><a href="https://en.wikipedia.org/wiki/Binary-coded_decimal" target="_blank" rel="noreferrer noopener nofollow">BCD numbers</a> are represented in a natural way, as we would see them; the hexadecimal 99 represents the decimal 99.</p>



<p class="has-medium-font-size wp-block-paragraph">To achieve this, a byte can represent numbers from 0 to 99 by dividing the byte into two nibbles (4 bits). Each nibble can represent numbers from 0 to 9.</p>



<p class="has-medium-font-size wp-block-paragraph">In this way, the hexadecimal 10, which in decimal is 16, in BCD represents the decimal 10. In other words, we see hexadecimal numbers as if they were decimal numbers.</p>



<h2 id="how-to-use-them" class="wp-block-heading has-large-font-size">How to use them?</h2>



<p class="has-medium-font-size wp-block-paragraph">To represent a 10-digit number, 5 bytes are needed. An extra byte can be used to indicate the position of the decimal point, in order to work with decimals. BCD numbers are used to operate with numbers of more than 16 bits.</p>



<p class="has-medium-font-size wp-block-paragraph">When operating with BCD numbers, the DAA (Decimal Adjust Accumulator) instruction must be taken into account. DAA makes adjustments to the results of operations with BCD numbers, and works as follows:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Check bits 0, 1, 2 and 3. If they contain a non-BCD digit (greater than 9) or the H flag is set, add or subtract, depending on the operation, 06h (0000 0110b) to the byte.</li>



<li class="has-medium-font-size">Check bits 4, 5, 6 and 7. If they contain a non-BCD digit (greater than 9) or the C flag is set, add or subtract, depending on the operation, 60h (0110 0000b) to the byte.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">Thus, if we have $09 and add $01, the result is $0A. After executing DAA, the result is $10. After each arithmetic instruction, increment or decrement, DAA must be executed.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld  a, $09    ; A = $09
inc a         ; A = $0A
daa           ; A = $10

dec a         ; A = $0F
daa           ; A = $09

add a, $03    ; A = $0C
daa           ; A = $12

sub $03       ; A = $0F
daa           ; A = $09" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld  a, </span><span style="color: #174781">$09</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; A = $09</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339"> a         </span><span style="color: #357B42; font-style: italic">; A = $0A</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">           </span><span style="color: #357B42; font-style: italic">; A = $10</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339"> a         </span><span style="color: #357B42; font-style: italic">; A = $0F</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">           </span><span style="color: #357B42; font-style: italic">; A = $09</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339"> a, </span><span style="color: #174781">$03</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; A = $0C</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">           </span><span style="color: #357B42; font-style: italic">; A = $12</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">sub</span><span style="color: #002339"> </span><span style="color: #174781">$03</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; A = $0F</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">           </span><span style="color: #357B42; font-style: italic">; A = $09</span></span></code></pre></div>



<h2 id="how-do-you-paint-them" class="wp-block-heading has-large-font-size">How do you paint them?</h2>



<p class="has-medium-font-size wp-block-paragraph">The way to obtain the ASCII code of each nibble (four bits) is relatively simple.</p>



<p class="has-medium-font-size wp-block-paragraph">The routine shown below receives a BCD number in register B and displays it on the screen.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -----------------------------------------------------
; Prints a BCD number on the screen.
; Input: B -&gt; BCD number to be printed.
; Alters the value of the AF register.
; ----------------------------------------------------- 
PrintBCD:
ld   a, b   ; Loads in A the number to print
and  $f0    ; Keeps the upper nibble
rrca
rrca
rrca
rrca        ; Rotates it four times to move it to the lower nibble
add  a, '0' ; Adds the ASCII of the 0 to get the ASCII of the number
rst  $10    ; Display the number on the screen (ZX Spectrum)
; call $bb5a  ; Display the number on the screen (Amstrad CPC)

ld   a, b   ; Reloads the number to be printed in A
and  $0f    ; Keeps the bottom nibble
add  a, '0' ; Adds the ASCII of the 0 to get the ASCII of the number
rst  $10    ; Display the number on the screen (ZX Spectrum)
; call $bb5a  ; Displays the number on the screen (Amstrad CPC)
 
ret         ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -----------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Prints a BCD number on the screen.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: B -&gt; BCD number to be printed.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF register.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ----------------------------------------------------- </span></span>
<span class="line"><span style="color: #7EB233">PrintBCD:</span></span>
<span class="line"><span style="color: #002339">ld   a, b   </span><span style="color: #357B42; font-style: italic">; Loads in A the number to print</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  $f0    </span><span style="color: #357B42; font-style: italic">; Keeps the upper nibble</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca</span></span>
<span class="line"><span style="color: #002339">rrca        </span><span style="color: #357B42; font-style: italic">; Rotates it four times to move it to the lower nibble</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, &#39;</span><span style="color: #174781">0</span><span style="color: #002339">&#39; </span><span style="color: #357B42; font-style: italic">; Adds the ASCII of the 0 to get the ASCII of the number</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; Display the number on the screen (ZX Spectrum)</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; call $bb5a  ; Display the number on the screen (Amstrad CPC)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, b   </span><span style="color: #357B42; font-style: italic">; Reloads the number to be printed in A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; Keeps the bottom nibble</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, &#39;</span><span style="color: #174781">0</span><span style="color: #002339">&#39; </span><span style="color: #357B42; font-style: italic">; Adds the ASCII of the 0 to get the ASCII of the number</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">    </span><span style="color: #357B42; font-style: italic">; Display the number on the screen (ZX Spectrum)</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; call $bb5a  ; Displays the number on the screen (Amstrad CPC)</span></span>
<span class="line"><span style="color: #002339"> </span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">         </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">RST $10, in ZX Spectrum, prints on screen the character corresponding to the Ascii code loaded in register A. CALL $BB5A does the same in Amstrad CPC.</p>



<h2 id="conclusion" class="wp-block-heading has-large-font-size">Conclusion</h2>



<p class="has-medium-font-size wp-block-paragraph">Working with this type of encoding is relatively simple and allows us to work with large numbers. In exchange, we need more bytes and more processing time.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-medium-font-size wp-block-paragraph">You can also visit the rest of tutorials:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Z80
<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/bcd-numbers/">BCD numbers</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/zx-spectrum-screen">ZX Spectrum screen</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-pong-0x0ff-introduction/">ZX-Pong</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-space-battle-0x00-introduction/">Space Battle</a></li>



<li class="has-medium-font-size"><a href="https://espamatica.com/es_ES/zx-spectrum-assembly-tic-tac-toe-0x01-game-board/">Tic-Tac-Toe</a></li>
</ul>
</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">And remember, if you use it, don&#8217;t just copy it, try to understand it and adapt it to your needs.</p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/bcd-numbers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5017</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Tic-Tac-Toe &#8211; 0x06 Final adjustment</title>
		<link>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Sat, 07 Oct 2023 06:40:45 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tic-Tac-Toe]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4579</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly, we will make the final adjustments. We have reached the last chapter. We are]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>, we will make the final adjustments.</p>



<p class="has-medium-font-size wp-block-paragraph">We have reached the last chapter. We are going to make a couple of tweaks that, while not entirely necessary, I think might be of interest.</p>



<p class="has-medium-font-size wp-block-paragraph">We will add an option in the menu to allow players to select the maximum number of tables they can have.</p>



<p class="has-medium-font-size wp-block-paragraph">As for the tables, currently it is necessary to make all possible moves (occupy all the cells) to complete the point, although sometimes we already know that it is not possible to win and it is a table. We are going to implement the detection of tables to detect when it is no longer possible to win and end the point.</p>



<p class="has-medium-font-size wp-block-paragraph">We will propose some modifications that can save some bytes and clock cycles.</p>



<p class="has-medium-font-size wp-block-paragraph">We will modify the Spectrum&#8217;s moves to make it harder to beat.</p>



<p class="has-medium-font-size wp-block-paragraph">Finally, we&#8217;ll add the loading screen.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#tables-menu">Tables menu</a>
</li>
<li><a href="#table-detection">Table detection</a>
</li>
<li><a href="#we-save-bytes-and-clock-cycles">We save bytes and clock cycles</a>

<ul>
<li><a href="#same-tile-for-both-players">Same tile for both players</a>
</li>
</ul>
</li>
<li><a href="#spectrum-movement">Spectrum movement</a>
</li>
<li><a href="#difficulty">Difficulty</a>
</li>
<li><a href="#loading-screen">Loading screen</a>
</li>
<li><a href="#zx-spectrum-assembly-tictactoe">ZX Spectrum Assembly, Tic-Tac-Toe</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul>


<h2 id="tables-menu" class="wp-block-heading has-large-font-size"><a></a><a></a>Tables menu</h2>



<p class="has-medium-font-size wp-block-paragraph">Currently, the maximum number of tables is declared in the MAXTIES constant in var.asm; we delete it.</p>



<p class="has-medium-font-size wp-block-paragraph">Locate MaxPoints and add the new variable for the tables below it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="MaxTies:     db $05        ; Maximum tables" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">MaxTies:</span><span style="color: #002339">     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Maximum tables</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate TitleEspamatica and add the Tables menu option just above it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="TitleOptionTies:   db $10, INK7, $16, $10, $08, &quot;4. &quot;, $10, INK6
                   defm &quot;Tables&quot;" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">TitleOptionTies:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">4.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Tables&quot;</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">To make it more symmetrical, we locate Title3InStripe and replace $16, $02, $09 at the end of the line with $16, $04, $09.</p>



<p class="has-medium-font-size wp-block-paragraph">In Main.asm, let&#8217;s add the handling of the new menu item. Locate menu_Time and replace JR NZ, menu_op two lines down:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jr   nz, menu_Ties         ; No, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">jr   nz, menu_Ties         </span><span style="color: #357B42; font-style: italic">; No, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate menu_TimeDo and three lines down, after JR menu_op, add the handling of the new menu item:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="menu_Ties:  
cp   KEY4                  ; Pushed 4?
jr   nz, menu_op           ; No, loop
ld   a, (MaxTies)          ; A = tables
add  a, $02                ; A += 2
cp   $0a                   ; A < 10?
jr   c, menu_TiesDo        ; Yes, skip
ld   a, $03                ; A = 3
menu_TiesDo:
ld   (MaxTies), a          ; Update tables
jr   menu_op               ; Loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">menu_Ties:</span><span style="color: #002339">  </span></span>
<span class="line"><span style="color: #002339">cp   KEY4                  </span><span style="color: #357B42; font-style: italic">; Pushed 4?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_op           </span><span style="color: #357B42; font-style: italic">; No, loop</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxTies)          </span><span style="color: #357B42; font-style: italic">; A = tables</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, </span><span style="color: #174781">$02</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A += 2</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$0a</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A &lt; 10?</span></span>
<span class="line"><span style="color: #002339">jr   c, menu_TiesDo        </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$03</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 3</span></span>
<span class="line"><span style="color: #7EB233">menu_TiesDo:</span></span>
<span class="line"><span style="color: #002339">ld   (MaxTies), a          </span><span style="color: #357B42; font-style: italic">; Update tables</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If the four key has been pressed, the maximum number of tables is increased by two, if it is greater than nine, it is reduced to three and loaded into memory.</p>



<p class="has-medium-font-size wp-block-paragraph">Both this range of values and the difference between one value and the other can be modified as required. You can also change it in points and time.</p>



<p class="has-medium-font-size wp-block-paragraph">Once we have the maximum number of tables in memory, we need to use them. Find the loop_reset tag, the LD B, MAXTIES line and replace it with the following:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (MaxTies)          ; A = maximum tables
ld   b, a                  ; B = A" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (MaxTies)          </span><span style="color: #357B42; font-style: italic">; A = maximum tables</span></span>
<span class="line"><span style="color: #002339">ld   b, a                  </span><span style="color: #357B42; font-style: italic">; B = A</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">All that remains is to print the selected value. In screen.asm, locate PrintOptions and add the following lines just above JP PrintBCD:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call PrintBCD              ; Paints it
ld   b, INI_TOP-$10        ; B = coord Y
call AT                    ; Position cursor
ld   hl, MaxTies           ; HL = time value" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$10</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxTies           </span><span style="color: #357B42; font-style: italic">; HL = time value</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load the emulator and check that we can now define the number of tables and that the game ends when the number of tables is reached.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cecb63ec&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cecb63ec" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="703" height="601" data-attachment-id="4581" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-01/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-01.png?fit=703%2C601&amp;ssl=1" data-orig-size="703,601" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S06-01" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-01.png?fit=703%2C601&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-01.png?resize=703%2C601&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4581" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-01.png?w=703&amp;ssl=1 703w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-01.png?resize=300%2C256&amp;ssl=1 300w" sizes="auto, (max-width: 703px) 100vw, 703px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="table-detection" class="wp-block-heading has-large-font-size">Table detection</h2>



<p class="has-medium-font-size wp-block-paragraph">Currently, in order for boards to be detected, all cells must be occupied and there must be no tic-tac-toe. In reality, it is possible to know if the point will end in a board before the board is full.</p>



<p class="has-medium-font-size wp-block-paragraph">We go to game.asm and implement table detection.</p>



<p class="has-medium-font-size wp-block-paragraph">To know if there is a possibility of a move, we just need to know if there is any combination left in which the cells are occupied by only one player or by no player at all.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Check for tables.
; In order to check if there are tables.
;
; Output: Z  -&gt; No tables.
;         NZ -&gt; There are tables.
;
; Alters the value of the AF, BC, DE, HL and IX registers.
; -------------------------------------------------------------------
CheckTies:
ld   b, $f0                ; B = single-player cell mask
ld   c, $0f                ; B = cell mask another player" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Check for tables.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; In order to check if there are tables.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Output: Z  -&gt; No tables.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;         NZ -&gt; There are tables.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC, DE, HL and IX registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">CheckTies:</span></span>
<span class="line"><span style="color: #002339">ld   b, $f0                </span><span style="color: #357B42; font-style: italic">; B = single-player cell mask</span></span>
<span class="line"><span style="color: #002339">ld   c, </span><span style="color: #174781">$0f</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = cell mask another player</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We load in B the mask to keep the cells occupied by one player, and in C the mask for the other player.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="checkTies_check123:
ld   hl, Grid              ; HL = grid address
ld   a, (hl)               ; A = cell value 1
inc  hl                    ; HL = cell address 2
add  a, (hl)               ; A+= cell value 2
inc  hl                    ; HL = cell address 3
add  a, (hl)               ; A+= cell value 2
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">checkTies_check123:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = grid address</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We place HL on the grid, add the value of the three cells in A and increment HL to move from one cell to another. We check if there are any cells occupied by a player, and if not, we leave. If so, we check if there are any cells occupied by the other player, and if not, we leave.</p>



<p class="has-medium-font-size wp-block-paragraph">The next two checks have the same structure.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="checkTies_check456:
inc  hl                    ; A = cell address 4
ld   a, (hl)               ; A = cell value 4
inc  hl                    ; HL = cell address 5
add  a, (hl)               ; A+= cell value 5
inc  hl                    ; HL = cell address 6
add  a, (hl)               ; A+= cell value 6
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits

checkTies_check789:
inc  hl                    ; A = cell address 7
ld   a, (hl)               ; A = cell value 7
inc  hl                    ; HL = cell address 8
add  a, (hl)               ; A+= cell value 8
inc  hl                    ; HL = cell address 9
add  a, (hl)               ; A+= cell value 9
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">checkTies_check456:</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; A = cell address 4</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">checkTies_check789:</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; A = cell address 7</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 9</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Look closely at how we go through the cells with HL, this helps us to save a few bytes in an implementation we did earlier.</p>



<p class="has-medium-font-size wp-block-paragraph">The following checks cannot be done by changing cells with HL, as they are not numerically contiguous; we use IX.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="checkTies_check147:
ld   ix, Grid-$01          ; IX = address Grid - 1
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$04)           ; A+= cell value 4
add  a, (ix+$07)           ; A+= cell value 7
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">checkTies_check147:</span></span>
<span class="line"><span style="color: #002339">ld   ix, Grid-</span><span style="color: #174781">$01</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; IX = address Grid - 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The rest of the checks follow the same structure.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="checkTies_check258:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$08)           ; A+= cell value 8
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits

checkTies_check369:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$06)           ; A+= cell value 6
add  a, (ix+$09)           ; A+= cell value 9
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits

checkTies_check159:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$09)           ; A+= cell value 9
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret  z                     ; None, exits

checkTies_check357:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$07)           ; A+= cell value 7
ld   d, a                  ; Preserve A in D
and  b                     ; A = cells occupied by a player
ret  z                     ; None, exits
ld   a, d                  ; Retrieves A from D
and  c                     ; A = cells occupied by another player
ret                        ; Exits with Z in correct state" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">checkTies_check258:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">checkTies_check369:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">checkTies_check159:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">checkTies_check357:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">ld   d, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by a player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; None, exits</span></span>
<span class="line"><span style="color: #002339">ld   a, d                  </span><span style="color: #357B42; font-style: italic">; Retrieves A from D</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; A = cells occupied by another player</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits with Z in correct state</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">With this we now have the prediction of whether there will be tables, all that remains is to go to main.asm and use it.</p>



<p class="has-medium-font-size wp-block-paragraph">Find loop_tie and three lines down substitute:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $09                ; A = 9
cp   (hl)                  ; Counter = 9?
jr   nz, loop_cont         ; No, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$09</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 9</span></span>
<span class="line"><span style="color: #002339">cp   (hl)                  </span><span style="color: #357B42; font-style: italic">; Counter = 9?</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_cont         </span><span style="color: #357B42; font-style: italic">; No, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call CheckTies             ; Any possible movement?
jr   z, loop_cont          ; No, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CheckTies             </span><span style="color: #357B42; font-style: italic">; Any possible movement?</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_cont          </span><span style="color: #357B42; font-style: italic">; No, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We no longer have to wait until the board is full to know if there are checkers or not. We call CheckTies and if there are no boards we continue with the point.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load the emulator and see the results.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cecb7870&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cecb7870" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="702" height="603" data-attachment-id="4583" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-02/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-02.png?fit=702%2C603&amp;ssl=1" data-orig-size="702,603" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S06-02" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-02.png?fit=702%2C603&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-02.png?resize=702%2C603&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4583" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-02.png?w=702&amp;ssl=1 702w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-02.png?resize=300%2C258&amp;ssl=1 300w" sizes="auto, (max-width: 702px) 100vw, 702px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">You may see some behaviour that makes you think something is wrong. You see rows one, two, three free, but you know that the Spectrum will occupy one cell, then you another, and so it should mark tables.</p>



<p class="has-medium-font-size wp-block-paragraph">The system is predicting, not guessing. Imagine that there are two free cells in a row and it is the turn of the player whose cell is not occupied to move. Well, he will occupy one of the cells, so we already know it is a draw, but imagine that the player rests on his laurels and loses his turn, the other player occupies another cell and there is only one free cell, and it is still not possible to predict draws, if the other player falls asleep again, he loses his turn again and the first player gets three in a row.</p>



<h2 id="we-save-bytes-and-clock-cycles" class="wp-block-heading has-large-font-size"><a></a><a></a>We save bytes and clock cycles</h2>



<p class="has-medium-font-size wp-block-paragraph">We will implement several modifications to save bytes and clock cycles.</p>



<p class="has-medium-font-size wp-block-paragraph">The first of these will be in game.asm, on the set of CheckWinner and ZxMove routines, applying the way we have implemented CheckTies to the horizontal lines, the numerically contiguous ones.</p>



<p class="has-medium-font-size wp-block-paragraph">Go to the CheckWinner_check tag and find the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+1)             ; A = cell 1
add  a, (ix+2)             ; A+= cell 2
add  a, (ix+3)             ; A+= cell 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">1</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">2</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">3</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And we replace them with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid              ; HL = cell address 1
ld   a, (hl)               ; A = cell value 1
inc  hl                    ; HL = cell address 2
add  a, (hl)               ; A+= cell value 2
inc  hl                    ; HL = cell address 3
add  a, (hl)               ; A+= cell value 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = cell address 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We locate the lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+4)             ; A = cell 4
add  a, (ix+5)             ; A+= cell 5
add  a, (ix+6)             ; A+= cell 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">4</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">5</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">6</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And we replace them with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="inc  hl                    ; HL = cell address 4
ld   a, (hl)               ; A = cell value 4
inc  hl                    ; HL = cell address 5
add  a, (hl)               ; A+= cell value 5
inc  hl                    ; HL = cell address 6
add  a, (hl)               ; A+= cell value 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 4</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We locate the lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+7)             ; A = cell 7
add  a, (ix+8)             ; A+= cell 8
add  a, (ix+9)             ; A+= cell 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">7</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A = cell 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">8</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">9</span><span style="color: #002339">)             </span><span style="color: #357B42; font-style: italic">; A+= cell 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And we replace them with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="inc  hl                    ; HL = cell address 7
ld   a, (hl)               ; A = cell value 7
inc  hl                    ; HL = cell address 8
add  a, (hl)               ; A+= cell value 8
inc  hl                    ; HL = cell address 9
add  a, (hl)               ; A+= cell value 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 7</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 9</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">This part is ready, add the comments of the routine to HL as the affected register.</p>



<p class="has-medium-font-size wp-block-paragraph">This is the comparison between the two versions:</p>



<figure class="wp-block-table"><table><tbody><tr><td>Version</td><td>Cycles</td><td>Bytes</td></tr><tr><td>1</td><td>688/641</td><td>118</td></tr><tr><td>2</td><td>638/591</td><td>111</td></tr></tbody></table><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">As you can see, we save fifty clock cycles and seven bytes. We compile, load into the emulator and see that it still works.</p>



<p class="has-medium-font-size wp-block-paragraph">We continue to modify ZxMove, specifically two parts of this routine set. We locate zxMoveToWin_123 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$02)           ; A+= cell value 2
add  a, (ix+$03)           ; A+= cell value 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid              ; HL = cell address 1
ld   a, (hl)               ; A = cell value 1
inc  hl                    ; HL = cell address 2
add  a, (hl)               ; A+= cell value 2
inc  hl                    ; HL = cell address 3
add  a, (hl)               ; A+= cell value 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = cell address 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate zxMoveToWin_456 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$04)           ; A = cell value 4
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$06)           ; A+= cell value 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$03          ; HL = cell address 4
ld   a, (hl)               ; A = cell value 4
inc  hl                    ; HL = cell address 5
add  a, (hl)               ; A+= cell value 5
inc  hl                    ; HL = cell address 6
add  a, (hl)               ; A+= cell value 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$03</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = cell address 4</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate zxMoveToWin_789 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$07)           ; A = cell value 7
add  a, (ix+$08)           ; A+= cell value 8
add  a, (ix+$09)           ; A+= cell value 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$06          ; HL = cell address 7
ld   a, (hl)               ; A = cell value 7
inc  hl                    ; HL = cell address 8
add  a, (hl)               ; A+= cell value 8
inc  hl                    ; HL = cell address 9
add  a, (hl)               ; A+= cell value 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$06</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = cell address 7</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 9</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We have now completed the first part. The main difference with CheckTies and CheckWinner is that although they are numerically contiguous cells, the move from cell three to cell four and from cell six to cell seven is not with INC HL, as ToMove changes the value of HL.</p>



<p class="has-medium-font-size wp-block-paragraph">Locate zxMoveAttack_123 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$02)           ; A+= cell value 2
add  a, (ix+$03)           ; A+= cell value 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid              ; HL = cell address 1
ld   a, (hl)               ; A = cell value 1
inc  hl                    ; HL = cell address 2
add  a, (hl)               ; A+= cell value 2
inc  hl                    ; HL = cell address 3
add  a, (hl)               ; A+= cell value 3" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = cell address 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate zxMoveAttack_456 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$04)           ; A = cell value 4
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$06)           ; A+= cell value 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$03          ; HL = cell address 4
ld   a, (hl)               ; A = cell value 4
inc  hl                    ; HL = cell address 5
add  a, (hl)               ; A+= cell value 5
inc  hl                    ; HL = cell address 6
add  a, (hl)               ; A+= cell value 6" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$03</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = cell address 4</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate zxMoveAttack_789 and replace it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$07)           ; A = cell value 7
add  a, (ix+$08)           ; A+= cell value 8
add  a, (ix+$09)           ; A+= cell value 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$06          ; HL = cell address 7
ld   a, (hl)               ; A = cell value 7
inc  hl                    ; HL = cell address 8
add  a, (hl)               ; A+= cell value 8
inc  hl                    ; HL = cell address 9
add  a, (hl)               ; A+= cell value 9" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$06</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; HL = cell address 7</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 9</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We make another modification in zxMoveDefence_cornerBlock34 and zxMoveDefence_cornerBlock67 that saves clock cycles but no bytes.</p>



<p class="has-medium-font-size wp-block-paragraph">Replacement in zxMoveDefence_cornerBlock34:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$04)           ; A+= cell value 4" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$02          ; A = cell address 3
ld   a, (hl)               ; A = cell value 3
inc  hl                    ; HL = cell address 4
add  a, (hl)               ; A+= cell value 4" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$02</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; A = cell address 3</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In zxMoveDefence_cornerBlock67 we replace:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (ix+$06)           ; A = cell value 6
add  a, (ix+$07)           ; A+= cell value 7" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Grid+$05          ; A = cell address 6
ld   a, (hl)               ; A = cell value 6
inc  hl                    ; HL = cell address 7
add  a, (hl)               ; A+= cell value 7" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Grid+</span><span style="color: #174781">$05</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; A = cell address 6</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = cell address 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (hl)               </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The changes in game.asm are done. As I said, add HL as affected registry in the comments of the routine, we forgot it before because the HL registry was already affected by the zxMoveGeneric routine.</p>



<p class="has-medium-font-size wp-block-paragraph">This is the comparison between the two versions of ZxMove:</p>



<figure class="wp-block-table"><table><tbody><tr><td>Version</td><td>Cycles</td><td>Bytes</td></tr><tr><td>1</td><td>4632/4079</td><td>808</td></tr><tr><td>2</td><td>4532/3979</td><td>802</td></tr></tbody></table><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Version two occupies six bytes less and takes a hundred cycles less than version one, which seems small when we compare it with the bytes saved by CheckWinner with fewer modifications. Remember that there the transition from cell three to cell four and from cell six to cell seven is done with INC HL, which we cannot do here.</p>



<p class="has-medium-font-size wp-block-paragraph">The difference between INC HL and LD HL, Grid is four clock cycles and two bytes, and that limits our savings.</p>



<h3 id="same-tile-for-both-players" class="wp-block-heading has-large-font-size">Same tile for both players</h3>



<p class="has-medium-font-size wp-block-paragraph">If we have capacity problems, which we don&#8217;t, we can save another handful of bytes by painting the same tile in a different colour for both players.</p>



<p class="has-medium-font-size wp-block-paragraph">We go to sprite.asm, comment out the Sprite_P2 definition and put the Sprite_CROSS, Sprite_SLASH and Sprite_MINUS definitions at the top of the file, like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; File: sprite.asm
;
; Definition of graphs.
; -------------------------------------------------------------------
; Crosshead Sprite
Sprite_CROSS:
db $18, $18, $18, $ff, $ff, $18, $18, $18 ; $90

; Sprite of the vertical line
Sprite_SLASH:
db $18, $18, $18, $18, $18, $18, $18, $18 ; $91

; Sprite of the horizontal line
Sprite_MINUS:
db $00, $00, $00, $ff, $ff, $00, $00, $00 ; $92

; Player 1 Sprite
Sprite_P1:
db $c0, $e0, $70, $38, $1c, $0e, $07, $03 ; $93
db $03, $07, $0e, $1c, $38, $70, $e0, $c0 ; $94

; ; Player Sprite 2
; Sprite_P2:
; db $03, $0f, $1c, $30, $60, $60, $c0, $c0 ; $95 Up/Left
; db $c0, $f0, $38, $0c, $06, $06, $03, $03 ; $96 Top/Right
; db $c0, $c0, $60, $60, $30, $1c, $0f, $03 ; $97 Down/Left
; db $03, $03, $06, $06, $0c, $38, $f0, $c0 ; $98 Down/Right" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; File: sprite.asm</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Definition of graphs.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Crosshead Sprite</span></span>
<span class="line"><span style="color: #7EB233">Sprite_CROSS:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, $ff, $ff, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $90</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite of the vertical line</span></span>
<span class="line"><span style="color: #7EB233">Sprite_SLASH:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$18</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $91</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite of the horizontal line</span></span>
<span class="line"><span style="color: #7EB233">Sprite_MINUS:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, $ff, $ff, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $92</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Player 1 Sprite</span></span>
<span class="line"><span style="color: #7EB233">Sprite_P1:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> $c0, $e0, </span><span style="color: #174781">$70</span><span style="color: #002339">, </span><span style="color: #174781">$38</span><span style="color: #002339">, </span><span style="color: #174781">$1c</span><span style="color: #002339">, </span><span style="color: #174781">$0e</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$03</span><span style="color: #002339"> </span><span style="color: #357B42; font-style: italic">; $93</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$03</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$0e</span><span style="color: #002339">, </span><span style="color: #174781">$1c</span><span style="color: #002339">, </span><span style="color: #174781">$38</span><span style="color: #002339">, </span><span style="color: #174781">$70</span><span style="color: #002339">, $e0, $c0 </span><span style="color: #357B42; font-style: italic">; $94</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ; Player Sprite 2</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sprite_P2:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; db $03, $0f, $1c, $30, $60, $60, $c0, $c0 ; $95 Up/Left</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; db $c0, $f0, $38, $0c, $06, $06, $03, $03 ; $96 Top/Right</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; db $c0, $c0, $60, $60, $30, $1c, $0f, $03 ; $97 Down/Left</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; db $03, $03, $06, $06, $0c, $38, $f0, $c0 ; $98 Down/Right</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Commenting on player two&#8217;s sprite not only forces us to change the routine that paints the tiles, it also forces us to change the routine that paints the board as the UDGs change. We have uploaded the board sprites above so that we can only change the board painting routine once, and so that if we decide to repaint the two tiles later, it won&#8217;t be affected.</p>



<p class="has-medium-font-size wp-block-paragraph">By commenting out Sprite_P2, we save thirty-two bytes. It doesn&#8217;t sound like much, but in certain situations it can save us.</p>



<p class="has-medium-font-size wp-block-paragraph">We go to var.asm and change the definition of Board_1 and _2 and leave them as they are:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; Vertical lines of the board.
Board_1:
db $12, $00, $13, $00        
db $20, $20, $20, $20, $91, $20, $20, $20, $20, $91, $20, $20, $20
db $20, $ff

; Horizontal lines of the board.
Board_2:        
db $92, $92, $92, $92, $90, $92, $92, $92, $92, $90, $92, $92, $92
db $92, $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; Vertical lines of the board.</span></span>
<span class="line"><span style="color: #7EB233">Board_1:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">        </span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$91</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$91</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$20</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$20</span><span style="color: #002339">, $ff</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Horizontal lines of the board.</span></span>
<span class="line"><span style="color: #7EB233">Board_2:</span><span style="color: #002339">        </span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$90</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$90</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$92</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$92</span><span style="color: #002339">, $ff</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and see the result, which is the mess we made.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cecba089&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cecba089" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="701" height="602" data-attachment-id="4588" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-03/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-03.png?fit=701%2C602&amp;ssl=1" data-orig-size="701,602" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S06-03" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-03.png?fit=701%2C602&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-03.png?resize=701%2C602&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4588" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-03.png?w=701&amp;ssl=1 701w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-03.png?resize=300%2C258&amp;ssl=1 300w" sizes="auto, (max-width: 701px) 100vw, 701px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">That player two&#8217;s tile is painted wrong is to be expected, but what about the board? We have changed the position of the sprite definition, but UDG is still pointing to Sprite_P1.</p>



<p class="has-medium-font-size wp-block-paragraph">We go to main.asm and under the Main tag we replace:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Sprite_P1         ; HL = address Sprite_P1" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Sprite_P1         </span><span style="color: #357B42; font-style: italic">; HL = address Sprite_P1</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">By:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Sprite_CROSS      ; HL = address Sprite_CROSS" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Sprite_CROSS      </span><span style="color: #357B42; font-style: italic">; HL = address Sprite_CROSS</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Also change the comment in the line below.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load it into the emulator and see that the board looks good, but the parts don&#8217;t. Don&#8217;t worry, this is to be expected.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cecbaeea&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cecbaeea" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="704" height="608" data-attachment-id="4589" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-04/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-04.png?fit=704%2C608&amp;ssl=1" data-orig-size="704,608" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S06-04" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-04.png?fit=704%2C608&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-04.png?resize=704%2C608&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4589" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-04.png?w=704&amp;ssl=1 704w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-04.png?resize=300%2C259&amp;ssl=1 300w" sizes="auto, (max-width: 704px) 100vw, 704px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">We are going to modify the routine that paints the tiles so that it does it well, or not, you can leave it as it is; three in a bloody line.</p>



<p class="has-medium-font-size wp-block-paragraph">We locate printOXO_X in screen.asm and the lines where the UDG is loaded in A. We add three to the value it loads:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph"><strong>ld a, $90 becomes ld a, $93<br>ld a, $91 becomes ld a, $94</strong></p>



<p class="has-medium-font-size wp-block-paragraph">We locate printOXO_Y and the lines on which the UDG is loaded in A. We add three to the value it loads:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph"><strong>ld a, $92 becomes ld a, $95<br>ld a, $93 becomes ld a, $96<br>ld a, $94 becomes ld a, $97<br>ld a, $95 becomes ld a, $98</strong></p>



<p class="has-medium-font-size wp-block-paragraph">So if we decided to paint two different tiles, the circle would be painted.</p>



<p class="has-medium-font-size wp-block-paragraph">As we are now in the situation of painting a single tile, we comment on the lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $95                ; A = 1st sprite
ld   a, $98                ; A = 4th sprite" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$95</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$98</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 4th sprite</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And below that we add the line:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $93                ; A = 1st sprite" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We comment on the lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $96                ; A = 2nd sprite
ld   a, $97                ; A = 3rd sprite" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$96</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$97</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 3rd sprite</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; And below that we add the line:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $94                ; A = 2nd sprite" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">A bit of a mess, isn&#8217;t it? Here&#8217;s what it looks like:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="printOXO_X:
ld   a, INKPLAYER1         ; A = ink player 1
call INK                   ; Change ink
ld   a, $93                ; A = 1st sprite
rst  $10                   ; Paints it
ld   a, $94                ; A = 2nd sprite
rst  $10                   ; Paints it
dec  b                     ; B = bottom line
call AT                    ; Position cursor
ld   a, $94                ; A = 2nd sprite
rst  $10                   ; Paints it
ld   a, $93                ; A = 2nd sprite
rst  $10                   ; Paints it
ret                        ; Exits

printOXO_Y:
ld   a, INKPLAYER2         ; A = ink player 2
call INK                   ; Change ink
; ld   a, $95                ; A = 1st sprite
ld   a, $93                ; A = 1st sprite
rst  $10                   ; Paints it
; ld   a, $96                ; A = 2nd sprite
ld   a, $94                ; A = 2nd sprite
rst  $10                   ; Paints it
dec  b                     ; B = bottom line
call AT                    ; Position cursor
; ld   a, $97                ; A = 3rd sprite
ld   a, $94                ; A = 2nd sprite
rst  $10                   ; Paints it
; ld   a, $98                ; A = 4th sprite
ld   a, $93                ; A = 1st sprite
rst  $10                   ; Paints it
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">printOXO_X:</span></span>
<span class="line"><span style="color: #002339">ld   a, INKPLAYER1         </span><span style="color: #357B42; font-style: italic">; A = ink player 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; B = bottom line</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">printOXO_Y:</span></span>
<span class="line"><span style="color: #002339">ld   a, INKPLAYER2         </span><span style="color: #357B42; font-style: italic">; A = ink player 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ld   a, $95                ; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ld   a, $96                ; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  b                     </span><span style="color: #357B42; font-style: italic">; B = bottom line</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ld   a, $97                ; A = 3rd sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$94</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2nd sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; ld   a, $98                ; A = 4th sprite</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$93</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1st sprite</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">So, if we want to redraw the two pieces, in sprite.asm we uncomment Sprite_P2, and in printOXO_Y we alternate the comments on the LD A, &#8230; lines.</p>



<p class="has-medium-font-size wp-block-paragraph">This will paint the pieces on the board, but not in the information part of the game.</p>



<p class="has-medium-font-size wp-block-paragraph">We go back to var.asm and modify player1_figure to look like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="player1_figure:   db $16, $04, $00, $90, $91, $0d, $91, $90" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">player1_figure:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$90</span><span style="color: #002339">, </span><span style="color: #174781">$91</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$91</span><span style="color: #002339">, </span><span style="color: #174781">$90</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And we leave it at that:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="player1_figure:   db $16, $04, $00, $93, $94, $0d, $94, $93" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">player1_figure:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$00</span><span style="color: #002339">, </span><span style="color: #174781">$93</span><span style="color: #002339">, </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$93</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We modify player2_figure so that it now looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="player2_figure:   db $16, $04, $1b, $92, $93, $16, $05, $1b
                  db $94, $95, $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">player2_figure:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$1b</span><span style="color: #002339">, </span><span style="color: #174781">$92</span><span style="color: #002339">, </span><span style="color: #174781">$93</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$1b</span></span>
<span class="line"><span style="color: #002339">                  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$95</span><span style="color: #002339">, $ff</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And we leave it at that:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="player2_figure:   db $16, $04, $1b, $93, $94, $16, $05, $1b
                  db $94, $93, $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">player2_figure:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$1b</span><span style="color: #002339">, </span><span style="color: #174781">$93</span><span style="color: #002339">, </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$1b</span></span>
<span class="line"><span style="color: #002339">                  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$94</span><span style="color: #002339">, </span><span style="color: #174781">$93</span><span style="color: #002339">, $ff</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Finally, we add the annotated definition for painting different tiles.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; player2_figure:   db $16, $04, $1b, $95, $96, $16, $05, $1b
;                   db $97, $98, $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; player2_figure:   db $16, $04, $1b, $95, $96, $16, $05, $1b</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;                   db $97, $98, $ff</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load in the emulator and see the results.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cecbc4dd&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cecbc4dd" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="700" height="606" data-attachment-id="4590" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-05/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-05.png?fit=700%2C606&amp;ssl=1" data-orig-size="700,606" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S06-05" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-05.png?fit=700%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-05.png?resize=700%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4590" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-05.png?w=700&amp;ssl=1 700w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-05.png?resize=300%2C260&amp;ssl=1 300w" sizes="auto, (max-width: 700px) 100vw, 700px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">You can paint the same tile in a different colour for both players, or you can paint a different colour for each player. If we were using a black and white TV, there would be no doubt.</p>



<p class="has-medium-font-size wp-block-paragraph">If you keep all the changes, we have saved forty-five bytes and one hundred and fifty clock cycles. If you decide to paint different tiles for each player, the byte saving is thirteen bytes.</p>



<h2 id="spectrum-movement" class="wp-block-heading has-large-font-size"><a></a><a></a>Spectrum movement</h2>



<p class="has-medium-font-size wp-block-paragraph">If you have played several games against the Spectrum, you will have worked out how to beat it every time you start the game, because there is at least one move that the Spectrum does not know how to defend, we have not programmed it.</p>



<p class="has-medium-font-size wp-block-paragraph">The specific move is to occupy two corner cells: two and six or six and eight. If we occupy cell two, the Spectrum occupies cell five, we move to cell six and the Spectrum moves to cell seven, we move to cell three and we have two tic-tac-toe moves: cells one, two and three and cells three, six and nine.</p>



<p class="has-medium-font-size wp-block-paragraph">In game.asm we locate zxMoveAttack_123 and above it we implement the lines that make it impossible to beat the Spectrum with this move.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Defensive corner movement.
; -------------------------------------------------------------------
zxMoveDefence_corner24:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$04)           ; A+= cell value 4
cp   b                     ; A = B?
jr   nz, zxMoveDefence_corner26 ; No, skip
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Defensive corner movement.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_corner24:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_corner26 </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We check if player one has occupied squares two and four, in which case we move to square one if possible.</p>



<p class="has-medium-font-size wp-block-paragraph">The rest of the checks follow the same structure.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMoveDefence_corner26:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$06)           ; A+= cell value 6
cp   b                     ; A = B?
jr   nz, zxMoveDefence_corner84 ; No, skip
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
zxMoveDefence_corner84:
ld   a, (ix+$08)           ; A = cell value 8
add  a, (ix+$04)           ; A+= cell value 4
cp   b                     ; A = B?
jr   nz, zxMoveDefence_corner86 ; No, skip
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
zxMoveDefence_corner86:
ld   a, (ix+$08)           ; A = cell value 8
add  a, (ix+$06)           ; A+= cell value 6
cp   b                     ; A = B?
jr   nz, zxMoveAttack_123  ; No, skip
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell nine
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMoveDefence_corner26:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_corner84 </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_corner84:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_corner86 </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_corner86:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_123  </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell nine</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Although the play is in two corners, we cover all four.</p>



<p class="has-medium-font-size wp-block-paragraph">Just above zxMoveDefence_cornerBlock2938Cont is the JR NZ line, zxMoveAttack_123, we modify it and leave it as it is:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jr   nz, zxMoveDefence_corner24 ; No, no mov cross, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_corner24 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If you compile and play some games against Spectrum, you will see that it is no longer possible to beat him with this move, but it is still possible to beat him, there are still moves that he does not know how to defend.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cecbd43c&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cecbd43c" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="701" height="604" data-attachment-id="4591" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/tictactoe-s06-06/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-06.png?fit=701%2C604&amp;ssl=1" data-orig-size="701,604" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S06-06" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-06.png?fit=701%2C604&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-06.png?resize=701%2C604&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4591" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-06.png?w=701&amp;ssl=1 701w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-06.png?resize=300%2C258&amp;ssl=1 300w" sizes="auto, (max-width: 701px) 100vw, 701px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="difficulty" class="wp-block-heading has-large-font-size">Difficulty</h2>



<p class="has-medium-font-size wp-block-paragraph">Now it is more difficult to beat the Spectrum, we have to wait for him to start the game and depending on the move he makes, we can beat him.</p>



<p class="has-medium-font-size wp-block-paragraph">We will add another option to the menu to be able to choose the level of difficulty: one to not cover the play in the corner, two to cover the play in the corner.</p>



<p class="has-medium-font-size wp-block-paragraph">In var.asm, after MaxTies, we add the variable for the level of difficulty:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="Level:       db $02        ; Difficulty level" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">Level:</span><span style="color: #002339">       </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$02</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Difficulty level</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">When a new menu item is added, it is appended to the Spamatica line. We move all the menu items up one line, like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="TitleOptionStart:  db $10, INK1, $13, $01, $16, $07, $08, &quot;0. &quot;
                   db $10, INK5
                   defm &quot;Start&quot;
TitleOptionPlayer: db $10, INK7, $13, $01, $16, $09, $08, &quot;1. &quot;
                   db $10, INK6
                   defm &quot;Players&quot;
TitleOptionPoint:  db $10, INK7, $16, $0b, $08, &quot;2. &quot;, $10, INK6
                   defm &quot;Points&quot;
TitleOptionTime:   db $10, INK7, $16, $0d, $08, &quot;3. &quot;, $10, INK6
                   defm &quot;Time&quot;
TitleOptionTies:   db $10, INK7, $16, $0f, $08, &quot;4. &quot;, $10, INK6
                   defm &quot;Tables&quot;" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">TitleOptionStart:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK1, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">0.</span><span style="color: #002339"> &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK5</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Start&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionPlayer:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$09</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">1.</span><span style="color: #002339"> &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Players&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionPoint:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">2.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Points&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionTime:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">3.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Time&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionTies:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0f</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">4.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Tables&quot;</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">After the definition of TitleOptionTies, we add the definition of Difficulty:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="TitleOptionLevel:  db $10, INK7, $16, $11, $08, &quot;5. &quot;, $10, INK6
                   defm &quot;Difficulty&quot;" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">TitleOptionLevel:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$11</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">5.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Difficulty&quot;</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In screen.asm we find PrintOptions and subtract one from all Y coordinate assignments:</p>



<p class="has-text-align-center has-pale-cyan-blue-background-color has-background has-medium-font-size wp-block-paragraph"><strong>LD B, INI_TOP-$0A go to LD B,INI_TOP-$09<br>LD B, INI_TOP-$0C go to LD B,INI_TOP-$0B<br>LD B, INI_TOP-$0E go to LD B,INI_TOP-$0D<br>LD B, INI_TOP-$10 go to LD B,INI_TOP-$0F</strong> </p>



<p class="has-medium-font-size wp-block-paragraph">Above JP PrintBCD we add the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call PrintBCD              ; Paints it
ld   b, INI_TOP-$11        ; B = coord Y
call AT                    ; Position cursor
ld   hl, Level             ; HL = difficulty value" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$11</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, Level             </span><span style="color: #357B42; font-style: italic">; HL = difficulty value</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The final aspect of the routine is as follows:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the values of the options.
;
; Alters the value of the AF, BC and HL registers.
; -------------------------------------------------------------------
PrintOptions:
ld   a, INK4               ; A = green ink
call INK                   ; Change ink
ld   b, INI_TOP-$09        ; B = coord Y
ld   c, INI_LEFT-$15       ; C = coord X
call AT                    ; Position cursor
ld   hl, MaxPlayers        ; HL = value players
call PrintBCD              ; Paints it
ld   b, INI_TOP-$0b        ; B = coord Y
call AT                    ; Position cursor
ld   hl, MaxPoints         ; HL = points value
call PrintBCD              ; Paints it
ld   b, INI_TOP-$0d        ; B = coord Y
call AT                    ; Position cursor
ld   hl, seconds           ; HL = time value
call PrintBCD              ; Paints it
ld   b, INI_TOP-$0f        ; B = coord Y
call AT                    ; Position cursor
ld   hl, MaxTies           ; HL = time value
call PrintBCD              ; Paints it
ld   b, INI_TOP-$11        ; B = coord Y
call AT                    ; Position cursor
ld   hl, Level             ; HL = difficulty value
jp   PrintBCD              ; Paints it in and exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the values of the options.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintOptions:</span></span>
<span class="line"><span style="color: #002339">ld   a, INK4               </span><span style="color: #357B42; font-style: italic">; A = green ink</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$09</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$15</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxPlayers        </span><span style="color: #357B42; font-style: italic">; HL = value players</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0b</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxPoints         </span><span style="color: #357B42; font-style: italic">; HL = points value</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0d</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, seconds           </span><span style="color: #357B42; font-style: italic">; HL = time value</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0f</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxTies           </span><span style="color: #357B42; font-style: italic">; HL = time value</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$11</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, Level             </span><span style="color: #357B42; font-style: italic">; HL = difficulty value</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it in and exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In main.asm find menu_Ties and then the line CP KEY4. After this line, replace JR NZ, menu_op with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jr   nz, menu_Level        ; No, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">jr   nz, menu_Level        </span><span style="color: #357B42; font-style: italic">; No, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">After menu_TiesDo, after the JR line menu_op, we add the processing lines of the new menu item:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="menu_Level:
cp   KEY5                  ; Pushed 5?
jr   nz, menu_op           ; No, loop
ld   a, (Level)            ; A = difficulty
xor  $03                   ; Alternates between 1 and 2
ld   (Level), a            ; Refresh in memory
jr   menu_op               ; Loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">menu_Level:</span></span>
<span class="line"><span style="color: #002339">cp   KEY5                  </span><span style="color: #357B42; font-style: italic">; Pushed 5?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_op           </span><span style="color: #357B42; font-style: italic">; No, loop</span></span>
<span class="line"><span style="color: #002339">ld   a, (Level)            </span><span style="color: #357B42; font-style: italic">; A = difficulty</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Alternates between 1 and 2</span></span>
<span class="line"><span style="color: #002339">ld   (Level), a            </span><span style="color: #357B42; font-style: italic">; Refresh in memory</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Finally, in game.asm we locate zxMoveDefence_corner24, and just below it we add:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (Level)            ; A = difficulty
cp   $01                   ; Difficulty = 1?
jr   z, zxMoveAttack_123   ; Yes, jumps" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (Level)            </span><span style="color: #357B42; font-style: italic">; A = difficulty</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Difficulty = 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveAttack_123   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">These lines do not check for corner play when the difficulty level is one. Compile and test.</p>



<h2 id="loading-screen" class="wp-block-heading has-large-font-size">Loading screen</h2>



<p class="has-medium-font-size wp-block-paragraph">Finally, we&#8217;re going to add the loading screen, but what&#8217;s the difference when we&#8217;ve already seen how it&#8217;s done in ZX-Pong and Space Battle? Well, this time we&#8217;re going to do it differently, because we&#8217;re not going to load the screen into VideoRAM, we&#8217;re going to load it into a different memory address and then make it appear all at once.</p>



<p class="has-medium-font-size wp-block-paragraph">This is the loading screen you can download from <a href="https://tinyurl.com/2qe2txpz" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801cecbe4b1&quot;}" data-wp-interactive="core/image" data-wp-key="6a801cecbe4b1" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="511" height="382" data-attachment-id="4509" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x01-game-board/tictactoe-s06-07/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-07.png?fit=511%2C382&amp;ssl=1" data-orig-size="511,382" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S06-07" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-07.png?fit=511%2C382&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-07.png?resize=511%2C382&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4509" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-07.png?w=511&amp;ssl=1 511w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S06-07.png?resize=300%2C224&amp;ssl=1 300w" sizes="auto, (max-width: 511px) 100vw, 511px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Please be kind, art has never been my thing. I will be happy if you design your own loading screen, which I am sure will be better than this one; it shouldn&#8217;t be difficult.</p>



<p class="has-medium-font-size wp-block-paragraph">In order to load the loading screen into a memory area and then dump it all at once into VideoRAM, we need to implement the routine to do this in a separate file and compile it separately.</p>



<p class="has-medium-font-size wp-block-paragraph">The loading process will do the following:</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Load the loader.</li>



<li class="has-medium-font-size">Load the routine that dumps the loading screen into memory address 24200.</li>



<li class="has-medium-font-size">Load the loading screen at address 24250.</li>



<li class="has-medium-font-size">Runs the routine that dumps the loading screen into VideoRAM.</li>



<li class="has-medium-font-size">Load the tic-tac-toe routine at address 24200.</li>



<li class="has-medium-font-size">Load the interrupt routine at address 32348.</li>



<li class="has-medium-font-size">Run the tic-tac-toe program.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">The first thing we are going to look at is the loader we developed in Basic to do all of the above.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">VB</span><span role="button" tabindex="0" data-code=" 10 CLEAR 24200
 20 INK 0: PAPER 4: BORDER 4: CLS
 30 POKE 23610,255: POKE 23739,111
 40 LOAD &quot;&quot;CODE 24200
 50 LOAD &quot;&quot;CODE 24250
 60 RANDOMIZE USR 24200
 70 LOAD &quot;&quot;CODE 24200
 80 LOAD &quot;&quot;CODE 32348
 90 CLS
100 RANDOMIZE USR 24200" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339"> </span><span style="color: #174781">10</span><span style="color: #002339"> </span><span style="color: #1AB394">CLEAR</span><span style="color: #002339"> </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">20</span><span style="color: #002339"> INK </span><span style="color: #174781">0</span><span style="color: #002339">: PAPER </span><span style="color: #174781">4</span><span style="color: #002339">: BORDER </span><span style="color: #174781">4</span><span style="color: #002339">: CLS</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">30</span><span style="color: #002339"> POKE </span><span style="color: #174781">23610</span><span style="color: #002339">,</span><span style="color: #174781">255</span><span style="color: #002339">: POKE </span><span style="color: #174781">23739</span><span style="color: #002339">,</span><span style="color: #174781">111</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">40</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">50</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">24250</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">60</span><span style="color: #0991B6"> RANDOMIZE </span><span style="color: #002339">USR </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">70</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">80</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">32348</span></span>
<span class="line"><span style="color: #002339"> </span><span style="color: #174781">90</span><span style="color: #002339"> CLS</span></span>
<span class="line"><span style="color: #174781">100</span><span style="color: #0991B6"> RANDOMIZE </span><span style="color: #002339">USR </span><span style="color: #174781">24200</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Don&#8217;t forget to save it with SAVE «OXO» LINE 10.</p>



<p class="has-medium-font-size wp-block-paragraph">The next step is to implement the routine that dumps the loading screen into the VideoRAM. We create the file loadScr.asm and add the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; LoadScr.asm
;
; The loading screen will be loaded in $5eba, and this routine will 
; be passed by the VideoRAM to appear all at once and will then 
; clear the area.
; Where it was initially loaded.
; The cleaning of this memory area is not necessary, but we do it.
; in case we need to debug, not to find code that actually are 
; remnants of the loading screen.
; -------------------------------------------------------------------
org  $5e88                 ; Loading address

ld   hl, $5eba             ; HL = address where the screen is located
ld   de, $4000             ; DE = VideoRAM address
ld   bc, $1b00             ; Screen length
ldir                       ; Flip the screen

ld   hl, $5eba             ; HL = address where the screen is located
ld   de, $5ebb             ; Next address
ld   bc, $1aff             ; Screen length - 1
ld   (hl), $00             ; Clear the first position
ldir                       ; Clean up the rest

ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; LoadScr.asm</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; The loading screen will be loaded in $5eba, and this routine will </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; be passed by the VideoRAM to appear all at once and will then </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; clear the area.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Where it was initially loaded.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; The cleaning of this memory area is not necessary, but we do it.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; in case we need to debug, not to find code that actually are </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; remnants of the loading screen.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$5e88</span><span style="color: #002339">                 </span><span style="color: #357B42; font-style: italic">; Loading address</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, </span><span style="color: #174781">$5eba</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; HL = address where the screen is located</span></span>
<span class="line"><span style="color: #002339">ld   de, </span><span style="color: #174781">$4000</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; DE = VideoRAM address</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$1b00</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Screen length</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Flip the screen</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, </span><span style="color: #174781">$5eba</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; HL = address where the screen is located</span></span>
<span class="line"><span style="color: #002339">ld   de, </span><span style="color: #174781">$5ebb</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Next address</span></span>
<span class="line"><span style="color: #002339">ld   bc, </span><span style="color: #174781">$1aff</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Screen length - 1</span></span>
<span class="line"><span style="color: #002339">ld   (hl), </span><span style="color: #174781">$00</span><span style="color: #002339">             </span><span style="color: #357B42; font-style: italic">; Clear the first position</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Clean up the rest</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The loading screen is loaded at $5eba. This routine copies $1B00 positions (6912 bytes), the entire area of pixels and attributes, from this address into VideoRAM.</p>



<p class="has-medium-font-size wp-block-paragraph">Once copied, we clean up the memory area where the screen was loaded; it is not necessary, but if we have to debug, we will avoid residual code that could confuse us.</p>



<p class="has-medium-font-size wp-block-paragraph">Now we just need to modify the script we need to compile and generate the final .tap. The Windows version looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ShellScript</span><span role="button" tabindex="0" data-code="echo off
cls
echo Compiling oxo
pasmo --name OXO --tap main.asm oxo.tap oxo.log
echo Compiling int
pasmo --name INT --tap int.asm int.tap int.log
echo Compiling loadscr
pasmo --name LoadScr --tap loadScr.asm loadscr.tap loadscr.log
echo Generating Tic-Tac-Toe
copy /b /y loader.tap+loadscr.tap+TresEnRayaScr.tap+oxo.tap+int.tap TicTacToe.tap" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">off</span></span>
<span class="line"><span style="color: #7EB233">cls</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">oxo</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">OXO</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">main.asm</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">int</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">INT</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.asm</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">LoadScr</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">loadScr.asm</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr.tap</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Generating</span><span style="color: #002339"> </span><span style="color: #A44185">Tic-Tac-Toe</span></span>
<span class="line"><span style="color: #7EB233">copy</span><span style="color: #002339"> </span><span style="color: #A44185">/b</span><span style="color: #002339"> </span><span style="color: #A44185">/y</span><span style="color: #002339"> </span><span style="color: #A44185">loader.tap+loadscr.tap+TresEnRayaScr.tap+oxo.tap+int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">TicTacToe.tap</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The Linux version would look like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ShellScript</span><span role="button" tabindex="0" data-code="clear
echo Compiling oxo
pasmo --name OXO --tap main.asm oxo.tap oxo.log
echo Compiling int
pasmo --name INT --tap int.asm int.tap int.log
echo Compiling loadscr
pasmo --name LoadScr --tap loadScr.asm loadscr.tap loadscr.log
echo Generating Tic-Tac-Toe
cat loader.tap loadscr.tap TresEnRayaScr.Tap oxo.tap int.tap &gt; TicTacToe.tap" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">clear</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">oxo</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">OXO</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">main.asm</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">int</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">INT</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.asm</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">LoadScr</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">loadScr.asm</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr.tap</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Generating</span><span style="color: #002339"> </span><span style="color: #A44185">Tic-Tac-Toe</span></span>
<span class="line"><span style="color: #7EB233">cat</span><span style="color: #002339"> </span><span style="color: #A44185">loader.tap</span><span style="color: #002339"> </span><span style="color: #A44185">loadscr.tap</span><span style="color: #002339"> </span><span style="color: #A44185">TresEnRayaScr.Tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #7B30D0">&gt;</span><span style="color: #002339"> </span><span style="color: #A44185">TicTacToe.tap</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">And that is it.</p>



<h2 id="zx-spectrum-assembly-tictactoe" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Tic-Tac-Toe</h2>



<p class="has-medium-font-size wp-block-paragraph">Thank you very much for joining me on this journey, I hope this is just the beginning, that it has helped you to learn and become passionate about assembly programming for the ZX Spectrum. I will continue to study and learn, and if I can gather more material in the future, I may write another tutorial.</p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/2bwwwsjw" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yprvqdhg" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<p class="wp-block-paragraph"></p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Tic-Tac-Toe by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309125309394" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309125309394/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x06-adjustments/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4579</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Tic-Tac-Toe &#8211; 0x05 Me versus the Spectrum</title>
		<link>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x05-me-versus-zx/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x05-me-versus-zx/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Fri, 06 Oct 2023 05:05:34 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tic-Tac-Toe]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4570</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly,&#160;we will implement the possibility to play against the Spectrum. Me versus the Spectrum Once]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>,&nbsp;we will implement the possibility to play against the Spectrum.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#me-versus-the-spectrum">Me versus the Spectrum</a>
</li>
<li><a href="#zx-spectrum-assembly-tictactoe">ZX Spectrum Assembly, Tic-Tac-Toe</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul>


<h2 id="me-versus-the-spectrum" class="wp-block-heading has-large-font-size">Me versus the Spectrum</h2>



<p class="has-medium-font-size wp-block-paragraph">Once we can play against friends and family, the possibility remains that we may not have anyone to play with, hence the need to be able to play against the Spectrum.</p>



<p class="has-medium-font-size wp-block-paragraph">We wrote the bulk of this implementation in game.asm, starting with a routine that generates semi-random numbers between one and nine, so that when the Spectrum starts the game, it does so in a different way.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Gets a semi-random number between 1 and 9.
;
; Return: A -&gt; Number obtained.
; -------------------------------------------------------------------
GetRandomN:
ld   a, r                  ; A = R
and  $0f                   ; Leave bits 0 to 3
inc  a                     ; A+=1
cp   $0a                   ; A &gt; 9?
ret  c                     ; No, exit
rra                        ; A/=2, because Carry = 0, otherwise SRL A
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Gets a semi-random number between 1 and 9.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Return: A -&gt; Number obtained.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">GetRandomN:</span></span>
<span class="line"><span style="color: #002339">ld   a, r                  </span><span style="color: #357B42; font-style: italic">; A = R</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Leave bits 0 to 3</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A+=1</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$0a</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A &gt; 9?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; No, exit</span></span>
<span class="line"><span style="color: #002339">rra                        </span><span style="color: #357B42; font-style: italic">; A/=2, because Carry = 0, otherwise SRL A</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">GetRandomN gets a semi-random number between one and nine, using register R. In the RRA line, as seen in the comments, we divide A by two because the carry is zero and because we only do it once, otherwise we would have to do it with SRL A. RRA is one byte and takes four clock cycles, SRL A is only twice as long. It is only divided by two if the number obtained is greater than 9.</p>



<p class="has-medium-font-size wp-block-paragraph">The part that carries out the movements of the spectrum is very long, as there are several combinations that are evaluated, so we will see it in blocks. However, as there are many similarities between the different blocks, we will only explain the most important ones.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Moves ZX Spectrum.
;
; Alters the value of the AF, BC and IX registers.
; -------------------------------------------------------------------
ZxMove:
ld   a, (MoveCounter)      ; A = moves
or   a                     ; Movements = 0?
jr   nz, zxMove_center     ; No, skip
call GetRandomN            ; A = number between 1 and 9
add  a, '0'                ; A = ascii code
ld   c, a                  ; C = A
call ToMove                ; Move to cell
ret                        ; Exits
zxMove_center:
cp   $01                   ; Moves &gt; 1?
jr   nz, zxMove_cont       ; Yes, skip
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Moves ZX Spectrum.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and IX registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">ZxMove:</span></span>
<span class="line"><span style="color: #002339">ld   a, (MoveCounter)      </span><span style="color: #357B42; font-style: italic">; A = moves</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Movements = 0?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMove_center     </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> GetRandomN            </span><span style="color: #357B42; font-style: italic">; A = number between 1 and 9</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, &#39;</span><span style="color: #174781">0</span><span style="color: #002339">&#39;                </span><span style="color: #357B42; font-style: italic">; A = ascii code</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; C = A</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"><span style="color: #7EB233">zxMove_center:</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Moves &gt; 1?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMove_cont       </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In this first part we decide if it is the first move of the game, and if so we take a random number between one and nine and move to that cell. If it is the second move, we try to move to cell five (middle). If these conditions are not met, we continue with the checks to see if the spectrum can win or lose.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMove_cont:
ld   ix, Grid-$01          ; IX = dir Grid-1
ld   b, $20                ; B = Spectrum value can earn
call zxMoveToWin_123       ; Move to gain Spectrum
ret  z                     ; If valid, exits
ld   b, $02                ; B = value player 1 can win
call zxMoveToWin_123       ; Move to avoid it
ret  z                     ; If valid, exits
jp   zxMoveDefence_diagonally ; Defensive movements" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMove_cont:</span></span>
<span class="line"><span style="color: #002339">ld   ix, Grid-</span><span style="color: #174781">$01</span><span style="color: #002339">          </span><span style="color: #357B42; font-style: italic">; IX = dir Grid-1</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$20</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = Spectrum value can earn</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> zxMoveToWin_123       </span><span style="color: #357B42; font-style: italic">; Move to gain Spectrum</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If valid, exits</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$02</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = value player 1 can win</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> zxMoveToWin_123       </span><span style="color: #357B42; font-style: italic">; Move to avoid it</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If valid, exits</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   zxMoveDefence_diagonally </span><span style="color: #357B42; font-style: italic">; Defensive movements</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Remember that the moves of player two, in this case the Spectrum, are signalled in bit four of the cells, so we load $20 into B, which would be the value if there were two cells occupied by the Spectrum in a row.</p>



<p class="has-medium-font-size wp-block-paragraph">We call zxMoveToWin_123 so that if Spectrum wins, it makes the move.</p>



<p class="has-medium-font-size wp-block-paragraph">If the Spectrum has not won, we load two into B to check if player one has the move to win. We call zxMoveToWin_123 a second time to prevent it from winning.</p>



<p class="has-medium-font-size wp-block-paragraph">If none of the above has happened, we go on the defensive.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Evaluates whether the Spectrum has the movement to win.
; -------------------------------------------------------------------
zxMoveToWin_123:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$02)           ; A+= cell value 2
add  a, (ix+$03)           ; A+= cell value 3
cp   b                     ; A = B?
jp   nz, zxMoveToWin_456   ; No, skip
; Spectrum can win
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
inc  c                     ; C = key 2
call ToMove                ; Move to cell 2
ret  z                     ; If correct, exits
inc  c                     ; C = key 3
call ToMove                ; Move to cell 3
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Evaluates whether the Spectrum has the movement to win.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveToWin_123:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   nz, zxMoveToWin_456   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Spectrum can win</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 2</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If the Spectrum occupies two squares, we try to move to square one, if not, to square two and if not, to square three. To go from one square to the next, we increment C, numerically they are contiguous.</p>



<p class="has-medium-font-size wp-block-paragraph">The checks for the combinations four, five, six and seven, eight, nine are the same as above.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMoveToWin_456:
ld   a, (ix+$04)           ; A = cell value 4
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$06)           ; A+= cell value 6
cp   b                     ; A = B?
jr   nz, zxMoveToWin_789   ; No, skip
; Spectrum can win
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits
inc  c                     ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
inc  c                     ; C = key 6
call ToMove                ; Move to cell 6
ret                        ; Sale

zxMoveToWin_789:
ld   a, (ix+$07)           ; A = cell value 7
add  a, (ix+$08)           ; A+= cell value 8
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A = B?
jr   nz, zxMoveToWin_147   ; No, skip
; Spectrum can win
Ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret  z                     ; If correct, exits
inc  c                     ; C = key 8
call ToMove                ; Move to cell 8
ret  z                     ; If correct, exits
inc  c                     ; C = key 9
call ToMove                ; Move to cell 9
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMoveToWin_456:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_789   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Spectrum can win</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Sale</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveToWin_789:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_147   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Spectrum can win</span></span>
<span class="line"><span style="color: #002339">Ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 8</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 8</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The rest of the tests change slightly.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMoveToWin_147:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$04)           ; A+= cell value 4
add  a, (ix+$07)           ; A+= cell value 7
cp   b                     ; A=B?
jr   nz, zxMoveToWin_258   ; No, skip
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMoveToWin_147:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A=B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_258   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">As the squares are not numerically contiguous, at the end we load each key instead of incrementing C. From here on, all the checks to see if the Spectrum can win or lose are the same.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMoveToWin_258:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$08)           ; A+= cell value 8
cp   b                     ; A=B?
jr   nz, zxMoveToWin_369   ; No, skip
ld   c, KEY2               ; C = key 2
call ToMove                ; Move to cell 2
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY8               ; C = key 8
call ToMove                ; Move to cell 8
ret                        ; Exits
    
zxMoveToWin_369:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$06)           ; A+= cell value 6
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A=B?
jr   nz, zxMoveToWin_159   ; No, skip
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
ld   c, KEY6               ; C = key 6
call ToMove                ; Move to cell 6
ret  z                     ; If correct, exits
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret                        ; Exits
    
zxMoveToWin_159:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A=B?
jr   nz, zxMoveToWin_357   ; No, skip
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret                        ; Exits

zxMoveToWin_357:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$07)           ; A+= cell value 7
cp   b                     ; A = B?
ret  nz
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMoveToWin_258:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A=B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_369   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY2               </span><span style="color: #357B42; font-style: italic">; C = key 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 2</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY8               </span><span style="color: #357B42; font-style: italic">; C = key 8</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 8</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"><span style="color: #002339">    </span></span>
<span class="line"><span style="color: #7EB233">zxMoveToWin_369:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A=B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_159   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY6               </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"><span style="color: #002339">    </span></span>
<span class="line"><span style="color: #7EB233">zxMoveToWin_159:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A=B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveToWin_357   </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveToWin_357:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  nz</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The set of routines zxMoveToWin is used both to know if Spectrum can win and make the move to do so, and to know if Spectrum can lose and make the move to avoid it.</p>



<p class="has-medium-font-size wp-block-paragraph">If both player one and Spectrum have no chance of winning, we try to anticipate player one&#8217;s move. First we check if the player makes a diagonal move that occupies both vertices, and if so we make a cross block.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Diagonal defensive movement.
; -------------------------------------------------------------------
zxMoveDefence_diagonally:
; Checks if player 1 has diagonal checkers
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A = B?
jr   z, zxMoveDefence_crossBlock ; Yes, mov diagonal, skip
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$07)           ; A+= cell value 7
cp   b                     ; A = B?
jr   nz, zxMoveDefence_crossBlock1 ; No, skip

zxMoveDefence_crossBlock:
; Cross locking
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits
ld   c, KEY6               ; C = key 6
call ToMove                ; Move to cell 6
ret  z                     ; If correct, exits
ld   c, KEY2               ; C = key 2
call ToMove                ; Move to cell 2
ret  z                     ; If correct, exits
ld   c, KEY8               ; C = key 8
call ToMove                ; Move to cell 8
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Diagonal defensive movement.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_diagonally:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Checks if player 1 has diagonal checkers</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_crossBlock </span><span style="color: #357B42; font-style: italic">; Yes, mov diagonal, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_crossBlock1 </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_crossBlock:</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Cross locking</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY6               </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY2               </span><span style="color: #357B42; font-style: italic">; C = key 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 2</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY8               </span><span style="color: #357B42; font-style: italic">; C = key 8</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 8</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Then we check if he has tried a diagonal move with the centre occupied, and if so, depending on which vertex is occupied, we block the vertical.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Defensive diagonal movement with the centre occupied.
; -------------------------------------------------------------------
zxMoveDefence_crossBlock1:
ld   a, (ix+$05)           ; A = cell value 5
and  $0f                   ; A = player value 1
jr   z, zxMoveDefence_cornerBlock16 ; Z = not occupied, skip
ld   a, (ix+$01)           ; A = cell value 1
and  $0f                   ; A = player value 1
jr   z, zxMoveDefence_crossBlock3 ; Z = not occupied, skip
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret  z                     ; If correct, exits

zxMoveDefence_crossBlock3:
ld   a, (ix+$03)           ; A = cell value 3
and  $0f                   ; A = player value 1
jr   z, zxMoveDefence_crossBlock7 ; Z = not occupied, skip
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret  z                     ; If correct, exits

zxMoveDefence_crossBlock7:
ld   a, (ix+$07)           ; A = cell value 7
and  $0f                   ; A = player value 1
jr   z, zxMoveDefence_crossBlock9 ; Z = not occupied, skip
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits

zxMoveDefence_crossBlock9:
ld   a, (ix+$09)           ; A = cell value 9
and  $0f                   ; A = player value 1
jr   z, zxMoveDefence_cornerBlock16 ; Z = not occupied, skip
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Defensive diagonal movement with the centre occupied.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_crossBlock1:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player value 1</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_cornerBlock16 </span><span style="color: #357B42; font-style: italic">; Z = not occupied, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player value 1</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_crossBlock3 </span><span style="color: #357B42; font-style: italic">; Z = not occupied, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_crossBlock3:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player value 1</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_crossBlock7 </span><span style="color: #357B42; font-style: italic">; Z = not occupied, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_crossBlock7:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player value 1</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_crossBlock9 </span><span style="color: #357B42; font-style: italic">; Z = not occupied, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_crossBlock9:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 9</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$0f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player value 1</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_cornerBlock16 </span><span style="color: #357B42; font-style: italic">; Z = not occupied, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If player one has not attempted a diagonal move, we will see if he has attempted a cross move and try to block it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Defensive cross movement.
; -------------------------------------------------------------------
zxMoveDefence_cornerBlock16:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$06)           ; A+= cell value 6
cp   b                     ; A = B?
jr   nz, zxMoveDefence_cornerBlock34 ; No, no mov cross, skip
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits

zxMoveDefence_cornerBlock34:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$04)           ; A+= cell value 4
cp   b                     ; A = B?
jr   nz, zxMoveDefence_cornerBlock67 ; No, no mov cross, skip
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits

zxMoveDefence_cornerBlock67:
ld   a, (ix+$06)           ; A = cell value 6
add  a, (ix+$07)           ; A+= cell value 7
cp   b                     ; A = B?
jr   nz, zxMoveDefence_cornerBlock49 ; No, no mov cross, skip
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret  z                     ; If correct, exits

zxMoveDefence_cornerBlock49:
ld   a, (ix+$04)           ; A = cell value 4
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A = B?
jr   nz, zxMoveDefence_cornerBlock1827 ; No, no mov cross, skip
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret  z                     ; If correct, exits

zxMoveDefence_cornerBlock1827:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$08)           ; A+= cell value 8
cp   b                     ; A = B?
jr   z, zxMoveDefence_cornerBlock1827Cont ; Yes, mov cross, block
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$07)           ; A+= Value cell 7
cp   b                     ; A = B?
jr   nz, zxMoveDefence_cornerBlock2938 ; No, no mov cross, skip

zxMoveDefence_cornerBlock1827Cont:
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits

zxMoveDefence_cornerBlock2938:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$09)           ; A+= cell value 9
cp   b                     ; A = B?
jr   z, zxMoveDefence_cornerBlock2938Cont ; Yes, mov cross, block
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$08)           ; A+= cell value 8
cp   b                     ; A = B?
jr   nz, zxMoveAttack_123  ; No, no mov cross, skip

zxMoveDefence_cornerBlock2938Cont:
ld   c, KEY6               ; C = key 6
call ToMove                ; Move to cell 6
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Defensive cross movement.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock16:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_cornerBlock34 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock34:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_cornerBlock67 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock67:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_cornerBlock49 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock49:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_cornerBlock1827 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock1827:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_cornerBlock1827Cont </span><span style="color: #357B42; font-style: italic">; Yes, mov cross, block</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= Value cell 7</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveDefence_cornerBlock2938 </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock1827Cont:</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock2938:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveDefence_cornerBlock2938Cont </span><span style="color: #357B42; font-style: italic">; Yes, mov cross, block</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_123  </span><span style="color: #357B42; font-style: italic">; No, no mov cross, skip</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveDefence_cornerBlock2938Cont:</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY6               </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If we&#8217;ve got this far, we&#8217;ve established that Spectrum has no move to win, that player one has no move to win, that player one is in no danger from player one&#8217;s diagonal or cross moves; it&#8217;s time for Spectrum to go on the attack.</p>



<p class="has-medium-font-size wp-block-paragraph">This last part will only be done in the first few moves, as the board fills up we will be more concerned with defending than attacking.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Horizontal, vertical and diagonal offensive movement.
; -------------------------------------------------------------------
zxMoveAttack_123:
ld   b, $20                ; B = Spectrum value with two boxes
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$02)           ; A+= cell value 2
add  a, (ix+$03)           ; A+= cell value 3
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_456  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
Jr   z, zxMoveAttack_456   ; Yes, jumps
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
inc  c                     ; C = key 2
call ToMove                ; Move to cell 2
ret  z                     ; If correct, exits
inc  c                     ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Horizontal, vertical and diagonal offensive movement.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_123:</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$20</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = Spectrum value with two boxes</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 3</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_456  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">Jr   z, zxMoveAttack_456   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 2</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">For the attack, we look for combinations where player one has no cells occupied and the Spectrum has only one.</p>



<p class="has-medium-font-size wp-block-paragraph">The rest of the checks are very similar to the previous one.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(3 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="zxMoveAttack_456:
ld   a, (ix+$04)           ; A = cell value 4
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$06)           ; A+= cell value 6
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_789  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
jr   z, zxMoveAttack_789   ; Yes, jumps
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits
inc  c                     ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
inc  c                     ; C = key 6
call ToMove                ; Move to cell 6
ret  z                     ; If correct, exits

zxMoveAttack_789:
ld   a, (ix+$07)           ; A = cell value 7
add  a, (ix+$08)           ; A+= cell value 8
add  a, (ix+$09)           ; A+= cell value 9
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_147  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
jr   z, zxMoveAttack_147   ; Yes, jumps
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret  z                     ; If correct, exits
inc  c                     ; C = key 8
call ToMove                ; Move to cell 8
ret  z                     ; If correct, exits
inc  c                     ; C = key 9
call ToMove                ; Move to cell 9
ret  z                     ; If correct, exits

zxMoveAttack_147:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$04)           ; A+= cell value 4
add  a, (ix+$07)           ; A+= cell value 7
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 boxes
jr   nz, zxMoveAttack_258  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
jr   z, zxMoveAttack_258   ; Yes, jumps
ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
ld   c, KEY4               ; C = key 4
call ToMove                ; Move to cell 4
ret  z                     ; If correct, exits
ld   c, KEY7               ; C = key 7
call ToMove                ; Move to cell 7
ret   z                    ; If correct, exits

zxMoveAttack_258:
ld   a, (ix+$02)           ; A = cell value 2
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$08)           ; A+= cell value 8
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_369  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
jr   z, zxMoveAttack_369   ; Yes, jumps
ld   c, KEY2               ; C = key 2
call ToMove                ; Move to cell 2
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY8               ; C = key 8
call ToMove                ; Move to cell 8
ret  z                     ; If correct, exits

zxMoveAttack_369:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$06)           ; A+= cell value 6
add  a, (ix+$09)           ; A+= cell value 9
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_159  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
Jr   z, zxMoveAttack_159   ; Yes, jumps
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
ld   c, KEY6               ; C = key 6
call ToMove                ; Move to cell 6
ret  z                     ; If correct, exits
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret  z                     ; If correct, exits

zxMoveAttack_159:
ld   a, (ix+$01)           ; A = cell value 1
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$09)           ; A+= cell value 9
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveAttack_357  ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
Jr   z, zxMoveAttack_357   ; Yes, jumps
Ld   c, KEY1               ; C = key 1
call ToMove                ; Move to cell 1
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY9               ; C = key 9
call ToMove                ; Move to cell 9
ret  z                     ; If correct, exits

zxMoveAttack_357:
ld   a, (ix+$03)           ; A = cell value 3
add  a, (ix+$05)           ; A+= cell value 5
add  a, (ix+$07)           ; A+= cell value 7
ld   c, a                  ; Preserve A in C
and  $03                   ; A = player 1 cells
jr   nz, zxMoveGeneric     ; Any? Yes, jump
ld   a, c                  ; Retrieves A
and  $30                   ; A = Spectrum boxes
cp   b                     ; Two busy?
jr   z, zxMoveGeneric      ; Yes, skip
ld   c, KEY3               ; C = key 3
call ToMove                ; Move to cell 3
ret  z                     ; If correct, exits
ld   c, KEY5               ; C = key 5
call ToMove                ; Move to cell 5
ret  z                     ; If correct, exits
ld   c, KEY7               ; C = key 7	
call ToMove                ; Move to cell 7
ret  z                     ; If correct, exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">zxMoveAttack_456:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_789  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveAttack_789   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_789:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 7</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_147  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveAttack_147   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 8</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 8</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_147:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$04</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 4</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 boxes</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_258  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveAttack_258   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY4               </span><span style="color: #357B42; font-style: italic">; C = key 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 4</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">   z                    </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_258:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$02</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 2</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$08</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 8</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_369  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveAttack_369   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY2               </span><span style="color: #357B42; font-style: italic">; C = key 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 2</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY8               </span><span style="color: #357B42; font-style: italic">; C = key 8</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 8</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_369:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$06</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 6</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_159  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">Jr   z, zxMoveAttack_159   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY6               </span><span style="color: #357B42; font-style: italic">; C = key 6</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 6</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_159:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$01</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 1</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$09</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 9</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveAttack_357  </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">Jr   z, zxMoveAttack_357   </span><span style="color: #357B42; font-style: italic">; Yes, jumps</span></span>
<span class="line"><span style="color: #002339">Ld   c, KEY1               </span><span style="color: #357B42; font-style: italic">; C = key 1</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 1</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY9               </span><span style="color: #357B42; font-style: italic">; C = key 9</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 9</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveAttack_357:</span></span>
<span class="line"><span style="color: #002339">ld   a, (ix+</span><span style="color: #174781">$03</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A = cell value 3</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$05</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 5</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, (ix+</span><span style="color: #174781">$07</span><span style="color: #002339">)           </span><span style="color: #357B42; font-style: italic">; A+= cell value 7</span></span>
<span class="line"><span style="color: #002339">ld   c, a                  </span><span style="color: #357B42; font-style: italic">; Preserve A in C</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = player 1 cells</span></span>
<span class="line"><span style="color: #002339">jr   nz, zxMoveGeneric     </span><span style="color: #357B42; font-style: italic">; Any? Yes, jump</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; Retrieves A</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$30</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = Spectrum boxes</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Two busy?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveGeneric      </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY3               </span><span style="color: #357B42; font-style: italic">; C = key 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 3</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY5               </span><span style="color: #357B42; font-style: italic">; C = key 5</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 5</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span>
<span class="line"><span style="color: #002339">ld   c, KEY7               </span><span style="color: #357B42; font-style: italic">; C = key 7	</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to cell 7</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; If correct, exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">When we arrive here, the Spectrum has not found a place to go and will move to the first free cell.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Generic movement. 
; If with all of the above, you have not made any movement
; moves to the first free cell.
; -------------------------------------------------------------------
zxMoveGeneric:
ld   c, '1'                ; C = ascii 1 (cell 1)
ld   b, $09                ; B = total cells
ld   a, $00                ; A = empty cell
ld   hl, Grid              ; HL = address 1st cell

zxMoveGeneric_loop:
cp   (hl)                  ; Free cell?
jr   z, zxMoveGeneric_end  ; Yes, skip
inc  c                     ; C = ascii next cell
inc  hl                    ; HL = next cell
djnz zxMoveGeneric_loop    ; Loop until B = 0

zxMoveGeneric_end:
call ToMove                ; Move to free cell
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Generic movement. </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; If with all of the above, you have not made any movement</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; moves to the first free cell.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">zxMoveGeneric:</span></span>
<span class="line"><span style="color: #002339">ld   c, &#39;</span><span style="color: #174781">1</span><span style="color: #002339">&#39;                </span><span style="color: #357B42; font-style: italic">; C = ascii 1 (cell 1)</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$09</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = total cells</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = empty cell</span></span>
<span class="line"><span style="color: #002339">ld   hl, Grid              </span><span style="color: #357B42; font-style: italic">; HL = address 1st cell</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveGeneric_loop:</span></span>
<span class="line"><span style="color: #002339">cp   (hl)                  </span><span style="color: #357B42; font-style: italic">; Free cell?</span></span>
<span class="line"><span style="color: #002339">jr   z, zxMoveGeneric_end  </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; C = ascii next cell</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = next cell</span></span>
<span class="line"><span style="color: #002339">djnz zxMoveGeneric_loop    </span><span style="color: #357B42; font-style: italic">; Loop until B = 0</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">zxMoveGeneric_end:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Move to free cell</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We have already implemented the way the Spectrum behaves in single player games. It&#8217;s not perfect, it has some loopholes and sometimes the Spectrum makes some erratic moves. That&#8217;s fine, if we made it perfect we could never beat the Spectrum and nobody likes that.</p>



<p class="has-medium-font-size wp-block-paragraph">Now we need to integrate what we have implemented inside the main loop into main.asm.</p>



<p class="has-medium-font-size wp-block-paragraph">Find the loop_lostMov tag, and five lines down replace JR Z, loop_keyCont with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jr   z, loop_players       ; Not active, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">jr   z, loop_players       </span><span style="color: #357B42; font-style: italic">; Not active, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate the loop_keyCont tag and replace the line above it, JR loop_cont, with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="jp   loop_cont             ; Jump" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   loop_cont             </span><span style="color: #357B42; font-style: italic">; Jump</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Between this line and loop_keyCont we add the Spectrum movement for single player games, this causes JR to give an out of range error, so we have replaced it with JP.</p>



<p class="has-medium-font-size wp-block-paragraph">Finally, between this line and loop_keyCont, we add the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="loop_players:
ld   a, (PlayerMoves)      ; A = player who moves
or   a                     ; Player 1?
jr   z, loop_keyCont       ; Yes, skip
ld   a, (MaxPlayers)       ; A = players
cp   $02                   ; 2 players?
jr   z, loop_keyCont       ; Yes, skip
call ZxMove                ; Move Spectrum
jr   nz, loop_players      ; NZ = incorrect, loop
push bc                    ; Preserve BC
ld   bc, SoundSpectrum     ; BC = sound address
call PlayMusic             ; Outputs sound
pop  bc                    ; Retrieves BC
jr   loop_print            ; Paints movement" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">loop_players:</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)      </span><span style="color: #357B42; font-style: italic">; A = player who moves</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_keyCont       </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPlayers)       </span><span style="color: #357B42; font-style: italic">; A = players</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$02</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; 2 players?</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_keyCont       </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ZxMove                </span><span style="color: #357B42; font-style: italic">; Move Spectrum</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_players      </span><span style="color: #357B42; font-style: italic">; NZ = incorrect, loop</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc                    </span><span style="color: #357B42; font-style: italic">; Preserve BC</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundSpectrum     </span><span style="color: #357B42; font-style: italic">; BC = sound address</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Outputs sound</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; Retrieves BC</span></span>
<span class="line"><span style="color: #002339">jr   loop_print            </span><span style="color: #357B42; font-style: italic">; Paints movement</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We get which player is moving, if it is two we get how many players there are and if it is one it moves the spectrum.</p>



<p class="has-medium-font-size wp-block-paragraph">Compile, load into the emulator and see the result. If all went well, we can now play against the Spectrum. If you think it is too easy, you can tweak the Spectrum&#8217;s movements, which we will do in the next chapter.</p>



<h2 id="zx-spectrum-assembly-tictactoe" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Tic-Tac-Toe</h2>



<p class="has-medium-font-size wp-block-paragraph">In the next chapter of ZX Spectrum Assembly, we will make the final adjustments.</p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/227mah88" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yprvqdhg" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<p class="wp-block-paragraph"></p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Tic-Tac-Toe by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309125309394" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309125309394/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x05-me-versus-zx/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4570</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Tic-Tac-Toe &#8211; 0x04 Options and end of game</title>
		<link>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Thu, 05 Oct 2023 05:17:28 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tic-Tac-Toe]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4554</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly,&#160;we will implement the different options of the game and the end of the game;]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>,&nbsp;we will implement the different options of the game and the end of the game; as we left off in the previous chapter, the game continues indefinitely. The options will be selected from a start menu.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#colours">Colours</a>
</li>
<li><a href="#menu">Menu</a>
</li>
<li><a href="#start-of-game">Start of game</a>
</li>
<li><a href="#shift-time">Shift time</a>
</li>
<li><a href="#end-of-game">End of game</a>
</li>
<li><a href="#zx-spectrum-assembly-tictactoe">ZX Spectrum Assembly, Tic-Tac-Toe</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul>


<h2 id="colours" class="wp-block-heading has-large-font-size">Colours</h2>



<p class="has-medium-font-size wp-block-paragraph">In var.asm we will add the following constants for the colours to avoid having to use numbers and to make it easier to change the colours during the game.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="INK0: equ $00
INK1: equ $01
INK2: equ $02
INK3: equ $03
INK4: equ $04
INK5: equ $05
INK6: equ $06
INK7: equ $07
INK0: equ $00
INK1: equ $01
INK2: equ $02
INK3: equ $03
INK4: equ $04
INK5: equ $05
INK6: equ $06
INK7: equ $07" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">INK0:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #7EB233">INK1:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #7EB233">INK2:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$02</span></span>
<span class="line"><span style="color: #7EB233">INK3:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$03</span></span>
<span class="line"><span style="color: #7EB233">INK4:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$04</span></span>
<span class="line"><span style="color: #7EB233">INK5:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$05</span></span>
<span class="line"><span style="color: #7EB233">INK6:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$06</span></span>
<span class="line"><span style="color: #7EB233">INK7:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$07</span></span>
<span class="line"><span style="color: #7EB233">INK0:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #7EB233">INK1:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #7EB233">INK2:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$02</span></span>
<span class="line"><span style="color: #7EB233">INK3:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$03</span></span>
<span class="line"><span style="color: #7EB233">INK4:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$04</span></span>
<span class="line"><span style="color: #7EB233">INK5:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$05</span></span>
<span class="line"><span style="color: #7EB233">INK6:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$06</span></span>
<span class="line"><span style="color: #7EB233">INK7:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$07</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">As an exercise, go through the title and text declarations and where you see $10, change the number that follows it to the corresponding INK tag.</p>



<p class="has-medium-font-size wp-block-paragraph">In the same way, look for INK calls in the rest of the files, and instead of loading the ink number in A, load the corresponding INK tag.</p>



<h2 id="menu" class="wp-block-heading has-large-font-size">Menu</h2>



<p class="has-medium-font-size wp-block-paragraph">Some of the start menu texts are already defined in ar.asm: the TitleEspamatica tag and from TitleOptionStart to TitleOptionTime.</p>



<p class="has-medium-font-size wp-block-paragraph">We will delete all these lines as we are going to reimplement it, adding colours and positions, and it will look like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="Title3InStripe:    db $10, INK2, $13, $01, $16, $02, $09
                   defm &quot;Three in a row&quot;
TitleOptionStart:  db $10, INK1, $13, $01, $16, $08, $08, &quot;0. &quot;
                   db $10, INK5
                   defm &quot;Start&quot;
TitleOptionPlayer: db $10, INK7, $13, $01, $16, $0a, $08, &quot;1. &quot;
                   db $10, INK6
                   defm &quot;Players&quot;
TitleOptionPoint:  db $10, INK7, $16, $0c, $08, &quot;2. &quot;, $10, INK6
                   defm &quot;Points&quot;
TitleOptionTime:   db $10, INK7, $16, $0e, $08, &quot;3. &quot;, $10, INK6
                   defm &quot;Time&quot;
TitleEspamatica:   db $16, $14, $08
                   db $10, INK2, &quot;E&quot;, $10, INK6, &quot;s&quot;
                   db $10, INK4, &quot;p&quot;, $10, INK5, &quot;a&quot; 
                   db $10, INK2, &quot;m&quot;, $10, INK6, &quot;a&quot;
                   db $10, INK4, &quot;t&quot;, $10, INK5, &quot;i&quot;
                   db $10, INK2, &quot;c&quot;, $10, INK6, &quot;a&quot;
                   db $10, INK7, &quot; 2019&quot;, $ff" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">Title3InStripe:</span><span style="color: #002339">    </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK2, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$02</span><span style="color: #002339">, </span><span style="color: #174781">$09</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Three </span><span style="color: #7B30D0">in</span><span style="color: #002339"> a row&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionStart:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK1, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">0.</span><span style="color: #002339"> &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK5</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Start&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionPlayer:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0a</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">1.</span><span style="color: #002339"> &quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Players&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionPoint:</span><span style="color: #002339">  </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0c</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">2.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Points&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleOptionTime:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$0e</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, &quot;</span><span style="color: #174781">3.</span><span style="color: #002339"> &quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6</span></span>
<span class="line"><span style="color: #002339">                   defm &quot;Time&quot;</span></span>
<span class="line"><span style="color: #7EB233">TitleEspamatica:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$14</span><span style="color: #002339">, </span><span style="color: #174781">$08</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK2, &quot;E&quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6, &quot;s&quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK4, &quot;p&quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK5, &quot;a&quot; </span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK2, &quot;m&quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6, &quot;a&quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK4, &quot;t&quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK5, &quot;i&quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK2, &quot;c&quot;, </span><span style="color: #174781">$10</span><span style="color: #002339">, INK6, &quot;a&quot;</span></span>
<span class="line"><span style="color: #002339">                   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$10</span><span style="color: #002339">, INK7, &quot; </span><span style="color: #174781">2019</span><span style="color: #002339">&quot;, $ff</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Now we have the definition of the start menu almost ready to paint, we just need a routine to paint the values of the options. We implement this in screen.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the values of the options.
;
; Alters the value of the AF, BC and HL registers.
; -------------------------------------------------------------------
PrintOptions:
ld   a, INK4               ; A = green ink
call INK                   ; Change ink
ld   b, INI_TOP-$0a        ; B = coord Y
ld   c, INI_LEFT-$15       ; C = coord X
call AT                    ; Positions cursor
ld   hl, MaxPlayers        ; HL = value players
call PrintBCD              ; Paints it
ld   b, INI_TOP-$0c        ; B = coord Y
call AT                    ; Positions cursor
ld   hl, MaxPoints         ; HL = points value
call PrintBCD              ; Paints it
ld   b, INI_TOP-$0e        ; B = coord Y
call AT                    ; Positions cursor
ld   hl, MaxSeconds        ; HL = time value
jp   PrintBCD              ; Paints it and exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the values of the options.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintOptions:</span></span>
<span class="line"><span style="color: #002339">ld   a, INK4               </span><span style="color: #357B42; font-style: italic">; A = green ink</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0a</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$15</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Positions cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxPlayers        </span><span style="color: #357B42; font-style: italic">; HL = value players</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0c</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Positions cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxPoints         </span><span style="color: #357B42; font-style: italic">; HL = points value</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0e</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Positions cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, MaxSeconds        </span><span style="color: #357B42; font-style: italic">; HL = time value</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints it and exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">PrintOptions paints the values of the options in green. It positions the cursor and paints each of the values.</p>



<p class="has-medium-font-size wp-block-paragraph">Let&#8217;s see what the start menu looks like. We locate Main in main.asm, and under CALL OPENCHAN we add these lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="Menu:
di                         ; Deactivate interruptions
im   $01                   ; Interruptions = mode 1
ei                         ; Activates interruptions" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">Menu:</span></span>
<span class="line"><span style="color: #174781">di</span><span style="color: #002339">                         </span><span style="color: #357B42; font-style: italic">; Deactivate interruptions</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Interruptions = mode 1</span></span>
<span class="line"><span style="color: #002339">ei                         </span><span style="color: #357B42; font-style: italic">; Activates interruptions</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We have added a label for the menu part, disabled the interrupts to switch to mode one and enabled them again. We will then use the interrupts in mode two, so these lines are needed here.</p>



<p class="has-medium-font-size wp-block-paragraph">At the bottom we find CALL CLS and then add the following lines:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, Title3InStripe    ; HL = address three-inline	
call PrintString           ; Paints the menu
menu_op:    
call PrintOptions          ; Paint the options
jr   menu_op               ; Menu loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, Title3InStripe    </span><span style="color: #357B42; font-style: italic">; HL = address three-inline	</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintString           </span><span style="color: #357B42; font-style: italic">; Paints the menu</span></span>
<span class="line"><span style="color: #7EB233">menu_op:</span><span style="color: #002339">    </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintOptions          </span><span style="color: #357B42; font-style: italic">; Paint the options</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Menu loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We draw the start screen and the values of the options. We remain in an infinite loop as this is where we will implement the menu logic.</p>



<p class="has-medium-font-size wp-block-paragraph">With the menu definition and a few lines we have painted it, but we still need to make it work.</p>



<p class="has-medium-font-size wp-block-paragraph">Compile it, load it into the emulator and see how it looks.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801ced093d5&quot;}" data-wp-interactive="core/image" data-wp-key="6a801ced093d5" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="698" height="605" data-attachment-id="4556" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/tictactoe-s04-01/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-01.png?fit=698%2C605&amp;ssl=1" data-orig-size="698,605" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S04-01" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-01.png?fit=698%2C605&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-01.png?resize=698%2C605&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4556" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-01.png?w=698&amp;ssl=1 698w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-01.png?resize=300%2C260&amp;ssl=1 300w" sizes="auto, (max-width: 698px) 100vw, 698px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">Before implementing the menu logic, we will implement a routine in game.asm that uses the ROM to read the keyboard and return the ASCII code of the last key pressed. This routine will be used for the menu options and for the player name query.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Wait for a key to be pressed and return its Ascii code.
;
; Output: A -&gt; Ascii code of the key.
;
; Alters the value of the AF and HL registers.
; -------------------------------------------------------------------
WaitKeyAlpha:
ld   hl, FLAGS_KEY         ; HL = address flag keyboard
set  $03, (hl)             ; Input mode L

; Loop until a key is obtained.
WaitKeyAlpha_loop:
bit  $05, (hl)             ; Key pressed?
jr   z, WaitKeyAlpha_loop  ; Not pressed, loop
res  $05, (hl)             ; Bit set to 0 for future inspections

; Gets the Ascii of the key pressed.
; Valid Ascii 12 ($0C), 13 ($0D) and from 32 ($20) to 127 ($7F)
; If the key pressed is Space, load ' ' in A
WaitKeyAlpha_loadKey:
ld   hl, LAST_KEY          ; HL = last key pressed address
ld   a, (hl)               ; A = last key pressed
cp   $80                   ; Ascii &gt; 127?
jr   nc, WaitKeyAlpha      ; Yes, invalid key, loop
cp   KEYDEL                ; Pushed Delete?
ret  z                     ; Yes, exit
cp   KEYENT                ; Press Enter?
ret  z                     ; Yes, exit
cp   KEYSPC                ; Powered Space?
jr   c, WaitKeyAlpha       ; Ascii < space, invalid, loop
ret                        ; Exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Wait for a key to be pressed and return its Ascii code.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Output: A -&gt; Ascii code of the key.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">WaitKeyAlpha:</span></span>
<span class="line"><span style="color: #002339">ld   hl, FLAGS_KEY         </span><span style="color: #357B42; font-style: italic">; HL = address flag keyboard</span></span>
<span class="line"><span style="color: #002339">set  </span><span style="color: #174781">$03</span><span style="color: #002339">, (hl)             </span><span style="color: #357B42; font-style: italic">; Input mode L</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Loop until a key is obtained.</span></span>
<span class="line"><span style="color: #7EB233">WaitKeyAlpha_loop:</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$05</span><span style="color: #002339">, (hl)             </span><span style="color: #357B42; font-style: italic">; Key pressed?</span></span>
<span class="line"><span style="color: #002339">jr   z, WaitKeyAlpha_loop  </span><span style="color: #357B42; font-style: italic">; Not pressed, loop</span></span>
<span class="line"><span style="color: #002339">res  </span><span style="color: #174781">$05</span><span style="color: #002339">, (hl)             </span><span style="color: #357B42; font-style: italic">; Bit set to 0 for future inspections</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Gets the Ascii of the key pressed.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Valid Ascii 12 ($0C), 13 ($0D) and from 32 ($20) to 127 ($7F)</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; If the key pressed is Space, load &#39; &#39; in A</span></span>
<span class="line"><span style="color: #7EB233">WaitKeyAlpha_loadKey:</span></span>
<span class="line"><span style="color: #002339">ld   hl, LAST_KEY          </span><span style="color: #357B42; font-style: italic">; HL = last key pressed address</span></span>
<span class="line"><span style="color: #002339">ld   a, (hl)               </span><span style="color: #357B42; font-style: italic">; A = last key pressed</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$80</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Ascii &gt; 127?</span></span>
<span class="line"><span style="color: #002339">jr   nc, WaitKeyAlpha      </span><span style="color: #357B42; font-style: italic">; Yes, invalid key, loop</span></span>
<span class="line"><span style="color: #002339">cp   KEYDEL                </span><span style="color: #357B42; font-style: italic">; Pushed Delete?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Yes, exit</span></span>
<span class="line"><span style="color: #002339">cp   KEYENT                </span><span style="color: #357B42; font-style: italic">; Press Enter?</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">  z                     </span><span style="color: #357B42; font-style: italic">; Yes, exit</span></span>
<span class="line"><span style="color: #002339">cp   KEYSPC                </span><span style="color: #357B42; font-style: italic">; Powered Space?</span></span>
<span class="line"><span style="color: #002339">jr   c, WaitKeyAlpha       </span><span style="color: #357B42; font-style: italic">; Ascii &lt; space, invalid, loop</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">WaitKeyAlpha waits until a key is pressed that is valid for us, that is: delete, enter or an ASCII code between thirty-two and one hundred and twenty-seven. When a valid key is pressed, it returns the ASCII code for that key in A.</p>



<p class="has-medium-font-size wp-block-paragraph">We implement the menu routine in main.asm, just after CALL PrintOptions, and the final aspect is this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="menu_op:
call PrintOptions          ; Paint the options
call WaitKeyAlpha          ; Wait for keypress
cp   KEY0                  ; Pushed 0?
jr   z, Init               ; Yes, start game
menu_Players:
cp   KEY1                  ; Pushed 1?
jr   nz, menu_Points       ; No, skip
ld   a, (MaxPlayers)       ; A = players
xor  $03                   ; Alternates between 1 and 2
ld   (MaxPlayers), a       ; Update in memory
jr   menu_op               ; Loop
menu_Points:
cp   KEY2                  ; Pushed 2?
jr   nz, menu_Time         ; No, skip
ld   a, (MaxPoints)        ; A = points
inc  a                     ; A += 1
cp   $06                   ; A = 6?
jr   nz, menu_PointsDo     ; No, skip
ld   a, $01                ; A = 1
menu_PointsDo:  
ld   (MaxPoints), a        ; Update in memory
add  a, '0'                ; A = value ascii
ld   (info_points), a      ; Update in memory
cp   '1'                   ; Points 1?
jr   z, menu_Points1       ; Yes, skip
ld   a, 's'                ; Plural
jr   menu_PointsEnd        ; Jump
menu_Points1:
ld   a, ' '                ; Singular
menu_PointsEnd:
ld   (info_gt1), a         ; Update in memory
jr   menu_op               ; Loop
menu_Time:
cp   KEY3                  ; Pushed 3?
jr   nz, menu_op           ; No, loop
ld   a, (MaxSeconds)       ; A = seconds
add  a, $05                ; A += 5
daa                        ; Decimal setting
cp   $35                   ; A = 35 BCD?
jr   nz, menu_TimeDo       ; No, skip
ld   a, $05                ; A = 5
menu_TimeDo:
ld   (MaxSeconds), a       ; Refreshes in memory
jr   menu_op               ; Loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">menu_op:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintOptions          </span><span style="color: #357B42; font-style: italic">; Paint the options</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyAlpha          </span><span style="color: #357B42; font-style: italic">; Wait for keypress</span></span>
<span class="line"><span style="color: #002339">cp   KEY0                  </span><span style="color: #357B42; font-style: italic">; Pushed 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, Init               </span><span style="color: #357B42; font-style: italic">; Yes, start game</span></span>
<span class="line"><span style="color: #7EB233">menu_Players:</span></span>
<span class="line"><span style="color: #002339">cp   KEY1                  </span><span style="color: #357B42; font-style: italic">; Pushed 1?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_Points       </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPlayers)       </span><span style="color: #357B42; font-style: italic">; A = players</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  </span><span style="color: #174781">$03</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Alternates between 1 and 2</span></span>
<span class="line"><span style="color: #002339">ld   (MaxPlayers), a       </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Loop</span></span>
<span class="line"><span style="color: #7EB233">menu_Points:</span></span>
<span class="line"><span style="color: #002339">cp   KEY2                  </span><span style="color: #357B42; font-style: italic">; Pushed 2?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_Time         </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPoints)        </span><span style="color: #357B42; font-style: italic">; A = points</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A += 1</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$06</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = 6?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_PointsDo     </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1</span></span>
<span class="line"><span style="color: #7EB233">menu_PointsDo:</span><span style="color: #002339">  </span></span>
<span class="line"><span style="color: #002339">ld   (MaxPoints), a        </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, &#39;</span><span style="color: #174781">0</span><span style="color: #002339">&#39;                </span><span style="color: #357B42; font-style: italic">; A = value ascii</span></span>
<span class="line"><span style="color: #002339">ld   (info_points), a      </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">cp   &#39;</span><span style="color: #174781">1</span><span style="color: #002339">&#39;                   </span><span style="color: #357B42; font-style: italic">; Points 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, menu_Points1       </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, &#39;s&#39;                </span><span style="color: #357B42; font-style: italic">; Plural</span></span>
<span class="line"><span style="color: #002339">jr   menu_PointsEnd        </span><span style="color: #357B42; font-style: italic">; Jump</span></span>
<span class="line"><span style="color: #7EB233">menu_Points1:</span></span>
<span class="line"><span style="color: #002339">ld   a, &#39; &#39;                </span><span style="color: #357B42; font-style: italic">; Singular</span></span>
<span class="line"><span style="color: #7EB233">menu_PointsEnd:</span></span>
<span class="line"><span style="color: #002339">ld   (info_gt1), a         </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Loop</span></span>
<span class="line"><span style="color: #7EB233">menu_Time:</span></span>
<span class="line"><span style="color: #002339">cp   KEY3                  </span><span style="color: #357B42; font-style: italic">; Pushed 3?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_op           </span><span style="color: #357B42; font-style: italic">; No, loop</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxSeconds)       </span><span style="color: #357B42; font-style: italic">; A = seconds</span></span>
<span class="line"><span style="color: #7B30D0">add</span><span style="color: #002339">  a, </span><span style="color: #174781">$05</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A += 5</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Decimal setting</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$35</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = 35 BCD?</span></span>
<span class="line"><span style="color: #002339">jr   nz, menu_TimeDo       </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$05</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 5</span></span>
<span class="line"><span style="color: #7EB233">menu_TimeDo:</span></span>
<span class="line"><span style="color: #002339">ld   (MaxSeconds), a       </span><span style="color: #357B42; font-style: italic">; Refreshes in memory</span></span>
<span class="line"><span style="color: #002339">jr   menu_op               </span><span style="color: #357B42; font-style: italic">; Loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">After calling the routine that checks if a valid key has been pressed, it evaluates if it is any key between zero and three, and depending on which it is, it acts in one way or another. Note that it only does the decimal adjustment when incrementing seconds; it is the only option whose value is greater than nine.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load the emulator and see the results. Everything seems to go well until we press zero, start the game and the menu remains.</p>



<p class="has-medium-font-size wp-block-paragraph">To fix this, we add the following line below the Init tag:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call CLS                   ; Clear screen" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLS                   </span><span style="color: #357B42; font-style: italic">; Clear screen</span></span></code></pre></div>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801ced0a163&quot;}" data-wp-interactive="core/image" data-wp-key="6a801ced0a163" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="703" height="605" data-attachment-id="4557" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/tictactoe-s04-02/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-02.png?fit=703%2C605&amp;ssl=1" data-orig-size="703,605" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S04-02" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-02.png?fit=703%2C605&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-02.png?resize=703%2C605&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4557" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-02.png?w=703&amp;ssl=1 703w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-02.png?resize=300%2C258&amp;ssl=1 300w" sizes="auto, (max-width: 703px) 100vw, 703px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="start-of-game" class="wp-block-heading has-large-font-size">Start of game</h2>



<p class="has-medium-font-size wp-block-paragraph">When the game starts, the first thing we will do is ask for the names of the players. First, we add another constant to rom.asm; this address contains the coordinates of the cursor.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; Cursor position on screen 2. 
; If loaded at BC -&gt; B = Y, C = X.
CURSOR: equ $5c88" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; Cursor position on screen 2. </span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; If loaded at BC -&gt; B = Y, C = X.</span></span>
<span class="line"><span style="color: #7EB233">CURSOR:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5c88</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We implement the routine responsible for retrieving the names of the players in game.asm. Although we won&#8217;t go through it instruction by instruction, due to the size of the routine we will implement it in blocks.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="GetPlayersName:
ld   hl, Name_p1      
ld   de, Name_p1+$01  
ld   bc, LENNAME*2-$01
ld   (hl), &quot; &quot; 
ldir    " style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">GetPlayersName:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p1      </span></span>
<span class="line"><span style="color: #002339">ld   de, Name_p1+</span><span style="color: #174781">$01</span><span style="color: #002339">  </span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME*</span><span style="color: #174781">2</span><span style="color: #002339">-</span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot; &quot; </span></span>
<span class="line"><span style="color: #002339">ldir    </span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We have cleared the names of the players.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   e, $01
getPlayersName_loop:
ld   a, INK4
call INK
ld   b, INI_TOP-$0f
ld   c, INI_LEFT-$01
call CLL
call AT

ld   hl, TitlePlayerNumber
ld   (hl), &quot;1&quot;
ld   a, $01
cp   e
jr   z, getPlayersName_cont
ld   (hl), &quot;2&quot;
getPlayersName_cont:
ld   hl, TitlePlayerName
call PrintString

ld   hl, Name_p1
ld   a, $01
cp   e
jr   z, getPlayersName_cont2
ld   hl, Name_p2" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   e, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_loop:</span></span>
<span class="line"><span style="color: #002339">ld   a, INK4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0f</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLL</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePlayerNumber</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot;</span><span style="color: #174781">1</span><span style="color: #002339">&quot;</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">cp   e</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_cont</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot;</span><span style="color: #174781">2</span><span style="color: #002339">&quot;</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_cont:</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePlayerName</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintString</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p1</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span></span>
<span class="line"><span style="color: #002339">cp   e</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_cont2</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We make a loop of at most two iterations, one per player, change the colour, delete the line where the names are requested, position the cursor, prepare the title depending on the player, and point HL to the player&#8217;s name.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="getPlayersName_cont2:
ld   d, $00
ld   a, INK3    
call INK   
call getPlayersName_getName 

ld   a, (MaxPlayers)           
cp   $02			 
jr   nz, getPlayersName_onlyOne
inc  e 
cp   e 
jr   z, getPlayersName_loop    
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">getPlayersName_cont2:</span></span>
<span class="line"><span style="color: #002339">ld   d, </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #002339">ld   a, INK3    </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK   </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> getPlayersName_getName </span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPlayers)           </span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$02</span><span style="color: #002339">			 </span></span>
<span class="line"><span style="color: #002339">jr   nz, getPlayersName_onlyOne</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  e </span></span>
<span class="line"><span style="color: #002339">cp   e </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_loop    </span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We use D to control the length of the name, change the ink and ask for the player&#8217;s name. We get the players, see if there are two players, and if not, default the name to two. If there are two players, we check if the name entered is the name of player two, and if not, we loop to ask for it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="getPlayersName_onlyOne:
ld   hl, Name_p2Default
ld   de, Name_p2       
ld   bc, LENNAME       
ldir    
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">getPlayersName_onlyOne:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2Default</span></span>
<span class="line"><span style="color: #002339">ld   de, Name_p2       </span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME       </span></span>
<span class="line"><span style="color: #002339">ldir    </span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If it is a single player, give the second player the default name.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="getPlayersName_getName:
push hl  
call WaitKeyAlpha            
pop  hl  

cp   KEYDEL      
jr   z, getPlayersName_delete 
cp   KEYENT      
jr   z, getPlayersName_enter		
push de  
ld   e, a 
ld   a, LENNAME       
cp   d 
ld   a, e 
pop  de 
jr   z, getPlayersName_getName
                                 
ld   (hl), a 
inc  hl 
rst  $10
inc  d 
jr   getPlayersName_getName" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">getPlayersName_getName:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl  </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyAlpha            </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl  </span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">cp   KEYDEL      </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_delete </span></span>
<span class="line"><span style="color: #002339">cp   KEYENT      </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_enter		</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> de  </span></span>
<span class="line"><span style="color: #002339">ld   e, a </span></span>
<span class="line"><span style="color: #002339">ld   a, LENNAME       </span></span>
<span class="line"><span style="color: #002339">cp   d </span></span>
<span class="line"><span style="color: #002339">ld   a, e </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  de </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName</span></span>
<span class="line"><span style="color: #002339">                                 </span></span>
<span class="line"><span style="color: #002339">ld   (hl), a </span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl </span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  d </span></span>
<span class="line"><span style="color: #002339">jr   getPlayersName_getName</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We wait for a valid key to be pressed, check if it is Delete and if so, we jump to its operation. We check if it is enter and if it is, we jump to its operation.</p>



<p class="has-medium-font-size wp-block-paragraph">If it is neither delete nor enter, we check if we have reached the maximum length and if not, we add the character to the name and draw it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="getPlayersName_delete:
ld   a, $00   
cp   d 
jr   z, getPlayersName_getName

dec  d 
dec  hl  
ld   a, ' ' 
ld   (hl), a 
ld   bc, (CURSOR)        
inc  c 
call AT  
rst  $10   
call AT  
jr   getPlayersName_getName" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">getPlayersName_delete:</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$00</span><span style="color: #002339">   </span></span>
<span class="line"><span style="color: #002339">cp   d </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  d </span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  hl  </span></span>
<span class="line"><span style="color: #002339">ld   a, &#39; &#39; </span></span>
<span class="line"><span style="color: #002339">ld   (hl), a </span></span>
<span class="line"><span style="color: #002339">ld   bc, (CURSOR)        </span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT  </span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">   </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT  </span></span>
<span class="line"><span style="color: #002339">jr   getPlayersName_getName</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If the key pressed is delete and the length of the name is not zero, we delete the previous character of the name and the display.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="getPlayersName_enter:
ld   a, 0 
cp   d 
jr   z, getPlayersName_getName
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">getPlayersName_enter:</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">0</span><span style="color: #002339"> </span></span>
<span class="line"><span style="color: #002339">cp   d </span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If the key pressed is enter and the length of the name is not zero, the name request is terminated.</p>



<p class="has-medium-font-size wp-block-paragraph">The final aspect of the routine is as follows:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(3 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Ask for the names of the players.
;
; Alters the value of the AF, BC, DE and HL registers.
; -------------------------------------------------------------------
GetPlayersName:
ld   hl, Name_p1           ; HL = address player name 1
ld   de, Name_p1+$01       ; DE = HL+1
ld   bc, LENNAME*2-$01     ; BC = length names - 1
ld   (hl), &quot; &quot;             ; Clears first position
ldir                       ; Clean up the rest

ld   e, $01                ; E = 1
getPlayersName_loop:
ld   a, INK4               ; A = ink 4
call INK                   ; Change ink
ld   b, INI_TOP-$0f        ; B = coord Y
ld   c, INI_LEFT-$01       ; X = coord X
call CLL                   ; Delete the line
call AT                    ; Position cursor

ld   hl, TitlePlayerNumber ; HL = player number
ld   (hl), &quot;1&quot;             ; Player 1
ld   a, $01                ; A = 1
cp   e                     ; Player 1?
jr   z, getPlayersName_cont; Yes, skip
ld   (hl), &quot;2&quot;             ; Player 2 
getPlayersName_cont:
ld   hl, TitlePlayerName   ; HL = title player name
call PrintString           ; Paints it

ld   hl, Name_p1           ; HL = address player name 1
ld   a, $01                ; A = 1
cp   e                     ; Player 1?
jr   z, getPlayersName_cont2 ; Yes, skip
ld   hl, Name_p2           ; HL = address player name 2
getPlayersName_cont2:
ld   d, $00                ; D = counter length name
ld   a, INK3               ; A = ink 3
call INK                   ; Change ink
call getPlayersName_getName; Request player name

ld   a, (MaxPlayers)       ; A = players
cp   $02                   ; Two players?
jr   nz, getPlayersName_onlyOne ; One player, default name
inc  e                     ; E+=1
cp   e                     ; Compare with players
jr   z, getPlayersName_loop; Equals, jumps
ret

; Single player
; Copies the default name of player 2
getPlayersName_onlyOne:
ld   hl, Name_p2Default    ; HL = default player name 2
ld   de, Name_p2           ; DE = name player 2
ld   bc, LENNAME           ; Length name
ldir                       ; Copy default name
ret                        ; Sale

; Requests the player's name
getPlayersName_getName:
push hl                    ; Preserve HL
call WaitKeyAlpha          ; Wait for valid key
pop  hl                    ; Retrieve HL

cp   KEYDEL                ; Delete?
jr   z, getPlayersName_delete ; Yes, skip
cp   KEYENT                ; Enter?
jr   z, getPlayersName_enter ; Yes, skip	
push de                    ; Preserves DE
ld   e, a                  ; E = code Ascii
ld   a, LENNAME            ; A = maximum length name
cp   d                     ; D = maximum length?
ld   a, e                  ; A = code Ascii
pop  de                    ; Retrieve DE
jr   z, getPlayersName_getName ; D = maximum length
                               ; other character
                               ; Enter or Delete

ld   (hl), a               ; Append character to name
inc  hl                    ; HL = next position
rst  $10                   ; Print character
inc  d                     ; D+=1
jr   getPlayersName_getName; Request another character

getPlayersName_delete:
ld   a, $00                ; A = 0
cp   d                     ; Length 0?
jr   z, getPlayersName_getName ; Yes, another character

dec  d                     ; D-=1
dec  hl                    ; HL-=1, previous character
ld   a, ' '                ; A = space
ld   (hl), a               ; Clear previous character
ld   bc, (CURSOR)          ; BC = cursor position
inc  c                     ; BC = previous column for AT
call AT                    ; Position cursor
rst  $10                   ; Delete the display character	
call AT                    ; Position cursor
jr   getPlayersName_getName; Other character

getPlayersName_enter:
ld   a, 0                  ; A = 0
cp   d                     ; Length 0?
jr   z, getPlayersName_getName ; Yes, another character
ret                        ; End name" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Ask for the names of the players.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC, DE and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">GetPlayersName:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p1           </span><span style="color: #357B42; font-style: italic">; HL = address player name 1</span></span>
<span class="line"><span style="color: #002339">ld   de, Name_p1+</span><span style="color: #174781">$01</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; DE = HL+1</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME*</span><span style="color: #174781">2</span><span style="color: #002339">-</span><span style="color: #174781">$01</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; BC = length names - 1</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot; &quot;             </span><span style="color: #357B42; font-style: italic">; Clears first position</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Clean up the rest</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   e, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; E = 1</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_loop:</span></span>
<span class="line"><span style="color: #002339">ld   a, INK4               </span><span style="color: #357B42; font-style: italic">; A = ink 4</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0f</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$01</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; X = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLL                   </span><span style="color: #357B42; font-style: italic">; Delete the line</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePlayerNumber </span><span style="color: #357B42; font-style: italic">; HL = player number</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot;</span><span style="color: #174781">1</span><span style="color: #002339">&quot;             </span><span style="color: #357B42; font-style: italic">; Player 1</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1</span></span>
<span class="line"><span style="color: #002339">cp   e                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_cont</span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   (hl), &quot;</span><span style="color: #174781">2</span><span style="color: #002339">&quot;             </span><span style="color: #357B42; font-style: italic">; Player 2 </span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_cont:</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePlayerName   </span><span style="color: #357B42; font-style: italic">; HL = title player name</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintString           </span><span style="color: #357B42; font-style: italic">; Paints it</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p1           </span><span style="color: #357B42; font-style: italic">; HL = address player name 1</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 1</span></span>
<span class="line"><span style="color: #002339">cp   e                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_cont2 </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2           </span><span style="color: #357B42; font-style: italic">; HL = address player name 2</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_cont2:</span></span>
<span class="line"><span style="color: #002339">ld   d, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; D = counter length name</span></span>
<span class="line"><span style="color: #002339">ld   a, INK3               </span><span style="color: #357B42; font-style: italic">; A = ink 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> getPlayersName_getName</span><span style="color: #357B42; font-style: italic">; Request player name</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPlayers)       </span><span style="color: #357B42; font-style: italic">; A = players</span></span>
<span class="line"><span style="color: #002339">cp   </span><span style="color: #174781">$02</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Two players?</span></span>
<span class="line"><span style="color: #002339">jr   nz, getPlayersName_onlyOne </span><span style="color: #357B42; font-style: italic">; One player, default name</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  e                     </span><span style="color: #357B42; font-style: italic">; E+=1</span></span>
<span class="line"><span style="color: #002339">cp   e                     </span><span style="color: #357B42; font-style: italic">; Compare with players</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_loop</span><span style="color: #357B42; font-style: italic">; Equals, jumps</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Single player</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Copies the default name of player 2</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_onlyOne:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2Default    </span><span style="color: #357B42; font-style: italic">; HL = default player name 2</span></span>
<span class="line"><span style="color: #002339">ld   de, Name_p2           </span><span style="color: #357B42; font-style: italic">; DE = name player 2</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME           </span><span style="color: #357B42; font-style: italic">; Length name</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Copy default name</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Sale</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Requests the player&#39;s name</span></span>
<span class="line"><span style="color: #7EB233">getPlayersName_getName:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl                    </span><span style="color: #357B42; font-style: italic">; Preserve HL</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyAlpha          </span><span style="color: #357B42; font-style: italic">; Wait for valid key</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; Retrieve HL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">cp   KEYDEL                </span><span style="color: #357B42; font-style: italic">; Delete?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_delete </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">cp   KEYENT                </span><span style="color: #357B42; font-style: italic">; Enter?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_enter </span><span style="color: #357B42; font-style: italic">; Yes, skip	</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> de                    </span><span style="color: #357B42; font-style: italic">; Preserves DE</span></span>
<span class="line"><span style="color: #002339">ld   e, a                  </span><span style="color: #357B42; font-style: italic">; E = code Ascii</span></span>
<span class="line"><span style="color: #002339">ld   a, LENNAME            </span><span style="color: #357B42; font-style: italic">; A = maximum length name</span></span>
<span class="line"><span style="color: #002339">cp   d                     </span><span style="color: #357B42; font-style: italic">; D = maximum length?</span></span>
<span class="line"><span style="color: #002339">ld   a, e                  </span><span style="color: #357B42; font-style: italic">; A = code Ascii</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  de                    </span><span style="color: #357B42; font-style: italic">; Retrieve DE</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName </span><span style="color: #357B42; font-style: italic">; D = maximum length</span></span>
<span class="line"><span style="color: #002339">                               </span><span style="color: #357B42; font-style: italic">; other character</span></span>
<span class="line"><span style="color: #002339">                               </span><span style="color: #357B42; font-style: italic">; Enter or Delete</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   (hl), a               </span><span style="color: #357B42; font-style: italic">; Append character to name</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = next position</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Print character</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  d                     </span><span style="color: #357B42; font-style: italic">; D+=1</span></span>
<span class="line"><span style="color: #002339">jr   getPlayersName_getName</span><span style="color: #357B42; font-style: italic">; Request another character</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">getPlayersName_delete:</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 0</span></span>
<span class="line"><span style="color: #002339">cp   d                     </span><span style="color: #357B42; font-style: italic">; Length 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName </span><span style="color: #357B42; font-style: italic">; Yes, another character</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  d                     </span><span style="color: #357B42; font-style: italic">; D-=1</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL-=1, previous character</span></span>
<span class="line"><span style="color: #002339">ld   a, &#39; &#39;                </span><span style="color: #357B42; font-style: italic">; A = space</span></span>
<span class="line"><span style="color: #002339">ld   (hl), a               </span><span style="color: #357B42; font-style: italic">; Clear previous character</span></span>
<span class="line"><span style="color: #002339">ld   bc, (CURSOR)          </span><span style="color: #357B42; font-style: italic">; BC = cursor position</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  c                     </span><span style="color: #357B42; font-style: italic">; BC = previous column for AT</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">rst  </span><span style="color: #174781">$10</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Delete the display character	</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">jr   getPlayersName_getName</span><span style="color: #357B42; font-style: italic">; Other character</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">getPlayersName_enter:</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">0</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; A = 0</span></span>
<span class="line"><span style="color: #002339">cp   d                     </span><span style="color: #357B42; font-style: italic">; Length 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, getPlayersName_getName </span><span style="color: #357B42; font-style: italic">; Yes, another character</span></span>
<span class="line"><span style="color: #7B30D0">ret</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; End name</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Let&#8217;s see if it works. In main.asm we find Init, and under CALL CLS we add the call to the name request, and another call to CLS.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call GetPlayersName        ; Request player names
call CLS                   ; Clear screen" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> GetPlayersName        </span><span style="color: #357B42; font-style: italic">; Request player names</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLS                   </span><span style="color: #357B42; font-style: italic">; Clear screen</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and see the results. We can now enter the names of the players and they will appear in the game information. If it is a single player game, we are playing against ZX Spectrum.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801ced0b475&quot;}" data-wp-interactive="core/image" data-wp-key="6a801ced0b475" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="704" height="606" data-attachment-id="4558" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/tictactoe-s04-03/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-03.png?fit=704%2C606&amp;ssl=1" data-orig-size="704,606" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S04-03" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-03.png?fit=704%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-03.png?resize=704%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4558" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-03.png?w=704&amp;ssl=1 704w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-03.png?resize=300%2C258&amp;ssl=1 300w" sizes="auto, (max-width: 704px) 100vw, 704px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="shift-time" class="wp-block-heading has-large-font-size">Shift time</h2>



<p class="has-medium-font-size wp-block-paragraph">One of the options is the number of seconds each player has to make their move. If the move has not been made within these seconds, the player loses the turn and it passes to the other player.</p>



<p class="has-medium-font-size wp-block-paragraph">We will use interrupts to control the time. We create the file int.asm.</p>



<p class="has-medium-font-size wp-block-paragraph">At the top of main.asm, just below ORG $5E88, we add the following lines (we already did this in Space Battle):</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Flags
; bit 0 -&gt; Reset countdown
; bit 1 -&gt; Lose turn
; bit 2 -&gt; Paint countdown
; bit 3 -&gt; Warning sound, countdown ends
; -------------------------------------------------------------------
flags:     db $00
; Value countdown
countdown: db $00
; Seconds per shift
seconds:   db $00" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Flags</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 0 -&gt; Reset countdown</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 1 -&gt; Lose turn</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 2 -&gt; Paint countdown</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 3 -&gt; Warning sound, countdown ends</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #174781">flags</span><span style="color: #002339">:     </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Value countdown</span></span>
<span class="line"><span style="color: #7EB233">countdown:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Seconds per shift</span></span>
<span class="line"><span style="color: #7EB233">seconds:</span><span style="color: #002339">   </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Seconds is used to tell the interrupts how many seconds per turn the players have chosen. With seconds, the MaxSeconds variable is no longer needed, so we will remove it.</p>



<p class="has-medium-font-size wp-block-paragraph">Locate Main and after the CALL OPENCHAN line, initialise seconds:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $10                ; A = $10 BCD
ld   (seconds), a          ; Update seconds
ld   (countdown), a        ; Update countdown" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$10</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = $10 BCD</span></span>
<span class="line"><span style="color: #002339">ld   (seconds), a          </span><span style="color: #357B42; font-style: italic">; Update seconds</span></span>
<span class="line"><span style="color: #002339">ld   (countdown), a        </span><span style="color: #357B42; font-style: italic">; Update countdown</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate menu_Time and after JR NZ, menu_op modify the line LD A, (MaxSeconds), as follows:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (seconds)          ; A = seconds" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (seconds)          </span><span style="color: #357B42; font-style: italic">; A = seconds</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate menu_TimeDo, delete LD, (MaxSecond), A and add the following lines instead:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   (seconds), a          ; Update seconds
ld   (countdown), a        ; Update countdown" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   (seconds), a          </span><span style="color: #357B42; font-style: italic">; Update seconds</span></span>
<span class="line"><span style="color: #002339">ld   (countdown), a        </span><span style="color: #357B42; font-style: italic">; Update countdown</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In screen.asm, at the end of the PrintOptions routine, we replace LD HL, MaxSeconds with:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   hl, seconds           ; HL = time value" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   hl, seconds           </span><span style="color: #357B42; font-style: italic">; HL = time value</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Finally, we delete the MaxSeconds definition in var.asm.</p>



<p class="has-medium-font-size wp-block-paragraph">The interrupt routine will change the value of the flags and the countdown, we need to take this into account in the main loop.</p>



<p class="has-medium-font-size wp-block-paragraph">For the interrupt routine to run fifty times per second (in PAL, sixty in NTSC), mode two of the interrupts must be enabled. Locate Loop in main.asm and add over it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="di                         ; Disables interruptions
ld   a, $28                ; A = $28
ld   i, a                  ; I = A (interruptions in $7e5c)
im   $02                   ; Interruptions = mode 2
ei                         ; Activates interruptions" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #174781">di</span><span style="color: #002339">                         </span><span style="color: #357B42; font-style: italic">; Disables interruptions</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$28</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = $28</span></span>
<span class="line"><span style="color: #002339">ld   i, a                  </span><span style="color: #357B42; font-style: italic">; I = A (interruptions in $7e5c)</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">$02</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Interruptions = mode 2</span></span>
<span class="line"><span style="color: #002339">ei                         </span><span style="color: #357B42; font-style: italic">; Activates interruptions</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We disable the interrupts, load $28 into the interrupt register, set it to mode two and enable it.</p>



<p class="has-medium-font-size wp-block-paragraph">We look for loop_key and just below it we implement the flag handling logic.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, (flags)            ; A = Flags
bit  $02, a                ; Bit 2 active?
res  $02, a                ; Disables bit 2
jr   z, loop_warning       ; Not active, skips
push af                    ; Preserve AF
call PrintCountDown        ; Paint countdown
pop  af                    ; Retrieve AF" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">flags</span><span style="color: #002339">)            </span><span style="color: #357B42; font-style: italic">; A = Flags</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$02</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Bit 2 active?</span></span>
<span class="line"><span style="color: #002339">res  </span><span style="color: #174781">$02</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Disables bit 2</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_warning       </span><span style="color: #357B42; font-style: italic">; Not active, skips</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> af                    </span><span style="color: #357B42; font-style: italic">; Preserve AF</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintCountDown        </span><span style="color: #357B42; font-style: italic">; Paint countdown</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  af                    </span><span style="color: #357B42; font-style: italic">; Retrieve AF</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We load the flags in A and evaluate if bit two is active and deactivate it. If it is not active, we jump, if it is, we draw the countdown.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="loop_warning:
bit  $03, a                ; Bit 3 active?             
res  $03, a                ; Disable bit 3
jr   z, loop_lostMov       ; Not active, skip
ld   bc, SoundCountDown    ; BC = sound direction 
call PlayMusic             ; Outputs sound" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">loop_warning:</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$03</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Bit 3 active?             </span></span>
<span class="line"><span style="color: #002339">res  </span><span style="color: #174781">$03</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Disable bit 3</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_lostMov       </span><span style="color: #357B42; font-style: italic">; Not active, skip</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundCountDown    </span><span style="color: #357B42; font-style: italic">; BC = sound direction </span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Outputs sound</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We evaluate whether bit three is active and deactivate it. If it is active, we jump, if it is not, we give the warning sound.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="loop_lostMov:
bit  $01, a                ; Bit 1 active?
res  $01, a                ; Disables bit 1
ld   (flags), a            ; Update in memory
halt                       ; Synchronise with interrupts
jr   z, loop_keyCont       ; Not active, skip
ld   hl, TitleLostMovement ; HL = address mov lost
ld   de, TitleLostMov_name ; DE = address name
call DoMsg                 ; Paint message
ld   bc, SoundLostMovement ; BC = sound direction
call PlayMusic             ; Outputs sound
jr   loop_cont             ; Jump
loop_keyCont:" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">loop_lostMov:</span></span>
<span class="line"><span style="color: #002339">bit  </span><span style="color: #174781">$01</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Bit 1 active?</span></span>
<span class="line"><span style="color: #002339">res  </span><span style="color: #174781">$01</span><span style="color: #002339">, a                </span><span style="color: #357B42; font-style: italic">; Disables bit 1</span></span>
<span class="line"><span style="color: #002339">ld   (</span><span style="color: #174781">flags</span><span style="color: #002339">), a            </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">halt                       </span><span style="color: #357B42; font-style: italic">; Synchronise with interrupts</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_keyCont       </span><span style="color: #357B42; font-style: italic">; Not active, skip</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleLostMovement </span><span style="color: #357B42; font-style: italic">; HL = address mov lost</span></span>
<span class="line"><span style="color: #002339">ld   de, TitleLostMov_name </span><span style="color: #357B42; font-style: italic">; DE = address name</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> DoMsg                 </span><span style="color: #357B42; font-style: italic">; Paint message</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundLostMovement </span><span style="color: #357B42; font-style: italic">; BC = sound direction</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Outputs sound</span></span>
<span class="line"><span style="color: #002339">jr   loop_cont             </span><span style="color: #357B42; font-style: italic">; Jump</span></span>
<span class="line"><span style="color: #7EB233">loop_keyCont:</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We check if bit one is active, disable it, update the flags and synchronise with the interrupts. If it is, we draw the lost motion message and play the sound. The loop_keyCont tag is above the CALL WaitKeyBoard.</p>



<p class="has-medium-font-size wp-block-paragraph">Finally, locate loop_cont, delete JR Loop and add the following lines in its place:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a, $01                ; A = restart countdown
ld   (flags), a            ; Update flags
jp   Loop                  ; Main loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$01</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = restart countdown</span></span>
<span class="line"><span style="color: #002339">ld   (</span><span style="color: #174781">flags</span><span style="color: #002339">), a            </span><span style="color: #357B42; font-style: italic">; Update flags</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   </span><span style="color: #7B30D0">Loop</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Main loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We have updated the flags so that the interrupt routine restarts the countdown. We also replaced JR with JP because the lines we added make JR out of range.</p>



<p class="has-medium-font-size wp-block-paragraph">In screen.asm we will implement the routine that draws the countdown.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Paint the countdown.
;
; Alters the value of the AF, BC and HL registers.
; -------------------------------------------------------------------
PrintCountDown:
ld   a, INK3               ; A = ink 3
call INK                   ; Change ink
ld   b, INI_TOP-$0c        ; B = coord Y
ld   c, INI_LEFT           ; C = coord X
call AT                    ; Position cursor
ld   hl, countdown         ; HL = direction countdown
call PrintBCD              ; Paint countdown left
ld   c, INI_LEFT-$1e       ; C = coord X
call AT                    ; Position cursor
jp   PrintBCD              ; Paints countdown right and exits" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Paint the countdown.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Alters the value of the AF, BC and HL registers.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PrintCountDown:</span></span>
<span class="line"><span style="color: #002339">ld   a, INK3               </span><span style="color: #357B42; font-style: italic">; A = ink 3</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> INK                   </span><span style="color: #357B42; font-style: italic">; Change ink</span></span>
<span class="line"><span style="color: #002339">ld   b, INI_TOP-</span><span style="color: #174781">$0c</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; B = coord Y</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT           </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #002339">ld   hl, countdown         </span><span style="color: #357B42; font-style: italic">; HL = direction countdown</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paint countdown left</span></span>
<span class="line"><span style="color: #002339">ld   c, INI_LEFT-</span><span style="color: #174781">$1e</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; C = coord X</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> AT                    </span><span style="color: #357B42; font-style: italic">; Position cursor</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   PrintBCD              </span><span style="color: #357B42; font-style: italic">; Paints countdown right and exits</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We paint the countdown in two positions, on the left and right of the board. We set the ink to magenta, move the cursor to the left, draw the countdown, move the cursor to the right and draw the countdown.</p>



<p class="has-medium-font-size wp-block-paragraph">The interrupt handling routine is implemented in int.asm (do not include this file in main.asm). We will also look at this implementation in blocks.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; int.asm
;
; Mode 2 Interrupt Handling
; -------------------------------------------------------------------
org $7e5c

; -------------------------------------------------------------------
; Indicators
; bit 0 -&gt; Reset countdown
; bit 1 -&gt; Lose turn
; bit 2 -&gt; Paint countdown
; bit 3 -&gt; Warning sound, countdown ends
; -------------------------------------------------------------------
FLAGS:     equ $5e88

COUNTDOWN: equ FLAGS+$01   ; Countdown Value
SECONDS:   equ FLAGS+$02   ; Seconds per turn" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; int.asm</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Mode 2 Interrupt Handling</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #002339">org </span><span style="color: #174781">$7e5c</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Indicators</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 0 -&gt; Reset countdown</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 1 -&gt; Lose turn</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 2 -&gt; Paint countdown</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; bit 3 -&gt; Warning sound, countdown ends</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #174781">FLAGS</span><span style="color: #002339">:     </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$5e88</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">COUNTDOWN:</span><span style="color: #002339"> </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">FLAGS</span><span style="color: #002339">+</span><span style="color: #174781">$01</span><span style="color: #002339">   </span><span style="color: #357B42; font-style: italic">; Countdown Value</span></span>
<span class="line"><span style="color: #7EB233">SECONDS:</span><span style="color: #002339">   </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">FLAGS</span><span style="color: #002339">+</span><span style="color: #174781">$02</span><span style="color: #002339">   </span><span style="color: #357B42; font-style: italic">; Seconds per turn</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The interrupt routine is loaded at address $7E5C. Then we add the constants with the memory addresses we will use for the information exchange between main.asm and int.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="CountDownISR:
push af  
push bc  
push de  
push hl  
push ix                    ; Preserves records" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">CountDownISR:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> af  </span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc  </span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> de  </span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl  </span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> ix                    </span><span style="color: #357B42; font-style: italic">; Preserves records</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The first step of the routine is to preserve the value of the registers.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="countDown_flags:
ld   a, (FLAGS)            ; A = flags
and  $01                   ; Reset countdown?
jr   z, countDown_cont     ; No, skip
ld   a, (SECONDS)          ; A = SECONDS
ld   (COUNTDOWN), a        ; Refreshes in memory
ld   a, $04                ; A = paint countdown
ld   (FLAGS), a            ; Update in memory
jr   countDownISR_end      ; End" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">countDown_flags:</span></span>
<span class="line"><span style="color: #002339">ld   a, (</span><span style="color: #174781">FLAGS</span><span style="color: #002339">)            </span><span style="color: #357B42; font-style: italic">; A = flags</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Reset countdown?</span></span>
<span class="line"><span style="color: #002339">jr   z, countDown_cont     </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   a, (SECONDS)          </span><span style="color: #357B42; font-style: italic">; A = SECONDS</span></span>
<span class="line"><span style="color: #002339">ld   (COUNTDOWN), a        </span><span style="color: #357B42; font-style: italic">; Refreshes in memory</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$04</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = paint countdown</span></span>
<span class="line"><span style="color: #002339">ld   (</span><span style="color: #174781">FLAGS</span><span style="color: #002339">), a            </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">jr   countDownISR_end      </span><span style="color: #357B42; font-style: italic">; End</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We check if main.asm indicates that the countdown should be restarted (shift change) and jump if it does not. If there is a shift, it restarts the countdown at the value specified in the menu, indicates in flags that the countdown should be painted, and jumps to exit the routine.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="countDown_cont:
ld   hl, countDownTicks    ; HL = ticks counter
inc  (hl)                  ; Counter ticks+=1
ld   a, $32                ; A = 50
cp   (hl)                  ; Counter ticks = 50?
jr   nz, countDownISR_end  ; No, skip.
xor  a                     ; A = 0, Z = 1, Carry = 0
ld   (hl), a               ; Counter ticks = 0" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">countDown_cont:</span></span>
<span class="line"><span style="color: #002339">ld   hl, countDownTicks    </span><span style="color: #357B42; font-style: italic">; HL = ticks counter</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)                  </span><span style="color: #357B42; font-style: italic">; Counter ticks+=1</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$32</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 50</span></span>
<span class="line"><span style="color: #002339">cp   (hl)                  </span><span style="color: #357B42; font-style: italic">; Counter ticks = 50?</span></span>
<span class="line"><span style="color: #002339">jr   nz, countDownISR_end  </span><span style="color: #357B42; font-style: italic">; No, skip.</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A = 0, Z = 1, Carry = 0</span></span>
<span class="line"><span style="color: #002339">ld   (hl), a               </span><span style="color: #357B42; font-style: italic">; Counter ticks = 0</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">In countDownTicks we add one at each pause, and when we reach fifty it is a sign that a second has passed, which we check in this block. If it has not reached fifty, it jumps to exit the routine, if it has, we set the ticks counter to zero and continue with the routine.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; It's reached 50, it's been a second.
ld   a, (COUNTDOWN)        ; A = countdown value
dec  a                     ; A-=1
daa                        ; Decimal setting
ld   (COUNTDOWN), a        ; Update in memory
ld   b, $04                ; B = paint countdown / 4 sec	
cp   b                     ; Less than 4 seconds?
jr   nc, countDownISR_reset; A &gt;= 4, skip
set  $03, b                ; Warning sound
or   a                     ; A = 0?
jr   nz, countDownISR_reset; No, skip
set  $01, b                ; Misses turn" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; It&#39;s reached 50, it&#39;s been a second.</span></span>
<span class="line"><span style="color: #002339">ld   a, (COUNTDOWN)        </span><span style="color: #357B42; font-style: italic">; A = countdown value</span></span>
<span class="line"><span style="color: #7B30D0">dec</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A-=1</span></span>
<span class="line"><span style="color: #7B30D0">daa</span><span style="color: #002339">                        </span><span style="color: #357B42; font-style: italic">; Decimal setting</span></span>
<span class="line"><span style="color: #002339">ld   (COUNTDOWN), a        </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span>
<span class="line"><span style="color: #002339">ld   b, </span><span style="color: #174781">$04</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; B = paint countdown / 4 sec	</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Less than 4 seconds?</span></span>
<span class="line"><span style="color: #002339">jr   nc, countDownISR_reset</span><span style="color: #357B42; font-style: italic">; A &gt;= 4, skip</span></span>
<span class="line"><span style="color: #002339">set  </span><span style="color: #174781">$03</span><span style="color: #002339">, b                </span><span style="color: #357B42; font-style: italic">; Warning sound</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; A = 0?</span></span>
<span class="line"><span style="color: #002339">jr   nz, countDownISR_reset</span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">set  </span><span style="color: #174781">$01</span><span style="color: #002339">, b                </span><span style="color: #357B42; font-style: italic">; Misses turn</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">If one second has passed, we calculate what kind of information to pass to main.asm. We subtract one second from the countdown, see if it is below four (it also communicates to main.asm that the countdown has to be painted), and jump if it is not. If it is, we set the bit for the warning sound, evaluate whether the countdown has reached zero, and jump if it has not. Turn on the shift loss bit if it has reached zero.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="countDownISR_reset:
ld   a, b                  ; A = B
ld   (FLAGS), a            ; Updates in memory" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">countDownISR_reset:</span></span>
<span class="line"><span style="color: #002339">ld   a, b                  </span><span style="color: #357B42; font-style: italic">; A = B</span></span>
<span class="line"><span style="color: #002339">ld   (</span><span style="color: #174781">FLAGS</span><span style="color: #002339">), a            </span><span style="color: #357B42; font-style: italic">; Updates in memory</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Update the value of flags with the information you want to pass to main.asm.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="countDownISR_end:
pop  ix     
pop  hl     
pop  de     
pop  bc     
pop  af                    ; Retrieves records
	

ei                         ; Activates interruptions
reti                       ; Exits

countDownTicks: db $00     ; Ticks (50*sec)" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">countDownISR_end:</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  ix     </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl     </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  de     </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc     </span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  af                    </span><span style="color: #357B42; font-style: italic">; Retrieves records</span></span>
<span class="line"><span style="color: #002339">	</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ei                         </span><span style="color: #357B42; font-style: italic">; Activates interruptions</span></span>
<span class="line"><span style="color: #002339">reti                       </span><span style="color: #357B42; font-style: italic">; Exits</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">countDownTicks:</span><span style="color: #002339"> </span><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span><span style="color: #002339">     </span><span style="color: #357B42; font-style: italic">; Ticks (50*sec)</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We get the value of the registers, enable interrupts and exit. Finally we declare the tick counter.</p>



<p class="has-medium-font-size wp-block-paragraph">Remember that we now need to compile two separate .tap&#8217;s and make our own loader.</p>



<p class="has-medium-font-size wp-block-paragraph">The basic loader looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">VB</span><span role="button" tabindex="0" data-code="10 CLEAR 24200
20 LOAD &quot;&quot;CODE 24200
30 LOAD &quot;&quot;CODE 32348
40 RANDOMIZE USR 24200" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #174781">10</span><span style="color: #002339"> </span><span style="color: #1AB394">CLEAR</span><span style="color: #002339"> </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #174781">20</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">24200</span></span>
<span class="line"><span style="color: #174781">30</span><span style="color: #002339"> LOAD </span><span style="color: #A44185">&quot;&quot;</span><span style="color: #002339">CODE </span><span style="color: #174781">32348</span></span>
<span class="line"><span style="color: #174781">40</span><span style="color: #0991B6"> RANDOMIZE </span><span style="color: #002339">USR </span><span style="color: #174781">24200</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The script to compile on Windows looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ShellScript</span><span role="button" tabindex="0" data-code="echo off
cls
echo Compiling oxo
pasmo --name OXO --tap main.asm oxo.tap oxo.log
echo Compiling int
pasmo --name INT --tap int.asm int.tap int.log
echo Generating Tic-tac-toe
copy /y /b loader.tap+oxo.tap+int.tap TicTacToe.tap
echo Process completed" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">off</span></span>
<span class="line"><span style="color: #7EB233">cls</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">oxo</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">OXO</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">main.asm</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">int</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">INT</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.asm</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Generating</span><span style="color: #002339"> </span><span style="color: #A44185">Tic-tac-toe</span></span>
<span class="line"><span style="color: #7EB233">copy</span><span style="color: #002339"> </span><span style="color: #A44185">/y</span><span style="color: #002339"> </span><span style="color: #A44185">/b</span><span style="color: #002339"> </span><span style="color: #A44185">loader.tap+oxo.tap+int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">TicTacToe.tap</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Process</span><span style="color: #002339"> </span><span style="color: #A44185">completed</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">The Linux version looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ShellScript</span><span role="button" tabindex="0" data-code="clear
echo Compiling oxo
pasmo --name OXO --tap main.asm oxo.tap oxo.log
echo Compiling int
pasmo --name INT --tap int.asm int.tap int.log
echo Generating Tic-tac-toe
cat loader.tap oxo.tap int.tap &gt; TicTacToe.tap
echo Process completed" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">clear</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">oxo</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">OXO</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">main.asm</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Compiling</span><span style="color: #002339"> </span><span style="color: #A44185">int</span></span>
<span class="line"><span style="color: #7EB233">pasmo</span><span style="color: #002339"> </span><span style="color: #174781">--name</span><span style="color: #002339"> </span><span style="color: #A44185">INT</span><span style="color: #002339"> </span><span style="color: #174781">--tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.asm</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.log</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Generating</span><span style="color: #002339"> </span><span style="color: #A44185">Tic-tac-toe</span></span>
<span class="line"><span style="color: #7EB233">cat</span><span style="color: #002339"> </span><span style="color: #A44185">loader.tap</span><span style="color: #002339"> </span><span style="color: #A44185">oxo.tap</span><span style="color: #002339"> </span><span style="color: #A44185">int.tap</span><span style="color: #002339"> </span><span style="color: #7B30D0">&gt;</span><span style="color: #002339"> </span><span style="color: #A44185">TicTacToe.tap</span></span>
<span class="line"><span style="color: #1AB394">echo</span><span style="color: #002339"> </span><span style="color: #A44185">Process</span><span style="color: #002339"> </span><span style="color: #A44185">completed</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile it, load it into the emulator and check that the countdown is painted, that an alarm goes off if it goes under four seconds and that it loses the turn if it goes to zero. We also see the missed turn message.</p>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801ced0d6dd&quot;}" data-wp-interactive="core/image" data-wp-key="6a801ced0d6dd" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="702" height="606" data-attachment-id="4561" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/tictactoe-s04-04/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-04.png?fit=702%2C606&amp;ssl=1" data-orig-size="702,606" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S04-04" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-04.png?fit=702%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-04.png?resize=702%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4561" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-04.png?w=702&amp;ssl=1 702w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-04.png?resize=300%2C259&amp;ssl=1 300w" sizes="auto, (max-width: 702px) 100vw, 702px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<h2 id="end-of-game" class="wp-block-heading has-large-font-size">End of game</h2>



<p class="has-medium-font-size wp-block-paragraph">At the end of this chapter we will implement the end of the game, which occurs when one of the players reaches the points defined in the menu, or when the maximum number of tables is reached.</p>



<p class="has-medium-font-size wp-block-paragraph">At the end of the game we are asked if we want another game; we add the keys of the answer as constants in var.asm. We also add the maximum number of tables.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="KEYN:       equ $4e        ; N key
KEYn:       equ $6e        ; Key n
KEYS:       equ $53        ; S key
KEYs:       equ $73        ; Key s
MAXTIES:    equ $05        ; Maximum number of tables" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">KEYN:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$4e</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; N key</span></span>
<span class="line"><span style="color: #7EB233">KEYn:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$6e</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key n</span></span>
<span class="line"><span style="color: #7EB233">KEYS:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$53</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; S key</span></span>
<span class="line"><span style="color: #7EB233">KEYs:</span><span style="color: #002339">       </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$73</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Key s</span></span>
<span class="line"><span style="color: #7EB233">MAXTIES:</span><span style="color: #002339">    </span><span style="color: #1AB394">equ</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">        </span><span style="color: #357B42; font-style: italic">; Maximum number of tables</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Depending on whether you want to play another game or not, you will jump to one place or another in main.asm. We locate the Init tag, and after CALL GetPlayersName we add the following tag:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="Start:" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">Start:</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We locate the loop_reset tag and just below it we will implement the check to see if someone has won the game.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="call PrintPoints           ; Paint the dots
ld   a, (MaxPoints)        ; A = maximum points
ld   b, a                  ; B = A
ld   a, (Points_p1)        ; A = points player 1
cp   b                     ; Player 1 wins?
jr   z, EndPlay            ; Yes, skip 
ld   a, (Points_p2)        ; A = points player 2
cp   b                     ; Player 2 wins?
jr   z, EndPlay            ; Yes, skip
ld   b, MAXTIES            ; B = maximum tables
ld   a, (Points_tie)       ; A = points tables
cp   b                     ; A = B?
jr   z, EndPlay            ; Yes, skip" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintPoints           </span><span style="color: #357B42; font-style: italic">; Paint the dots</span></span>
<span class="line"><span style="color: #002339">ld   a, (MaxPoints)        </span><span style="color: #357B42; font-style: italic">; A = maximum points</span></span>
<span class="line"><span style="color: #002339">ld   b, a                  </span><span style="color: #357B42; font-style: italic">; B = A</span></span>
<span class="line"><span style="color: #002339">ld   a, (Points_p1)        </span><span style="color: #357B42; font-style: italic">; A = points player 1</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Player 1 wins?</span></span>
<span class="line"><span style="color: #002339">jr   z, EndPlay            </span><span style="color: #357B42; font-style: italic">; Yes, skip </span></span>
<span class="line"><span style="color: #002339">ld   a, (Points_p2)        </span><span style="color: #357B42; font-style: italic">; A = points player 2</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; Player 2 wins?</span></span>
<span class="line"><span style="color: #002339">jr   z, EndPlay            </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #002339">ld   b, MAXTIES            </span><span style="color: #357B42; font-style: italic">; B = maximum tables</span></span>
<span class="line"><span style="color: #002339">ld   a, (Points_tie)       </span><span style="color: #357B42; font-style: italic">; A = points tables</span></span>
<span class="line"><span style="color: #002339">cp   b                     </span><span style="color: #357B42; font-style: italic">; A = B?</span></span>
<span class="line"><span style="color: #002339">jr   z, EndPlay            </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We draw the points and compare the players&#8217; scores with the set maximum score. If one of the players has the required number of points, they win the game.</p>



<p class="has-medium-font-size wp-block-paragraph">At the end of main.asm, before the includes, we add the split end routine.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="EndPlay:
di                         ; Disables interrupts
im   $01                   ; Mode 1 interrupts
ei                         ; Activates interruptions
ld   hl, TitleGameOver     ; HL = title game over
call PrintMsg              ; Paints the message
endPlay_waitKey:
call WaitKeyAlpha          ; Wait key
cp   KEYN                  ; Pressed N?
jp   z, Menu               ; Yes, menu
cp   KEYn                  ; Pushed n?
jp   z, Menu               ; Yes, menu
cp   KEYS                  ; Pulsed S?
jp   z, Start              ; Yes, start
cp   KEYs                  ; Pushed s?
jp   z, Start              ; Yes, start
jr   endPlay_waitKey       ; Not pressed, loop" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #7EB233">EndPlay:</span></span>
<span class="line"><span style="color: #174781">di</span><span style="color: #002339">                         </span><span style="color: #357B42; font-style: italic">; Disables interrupts</span></span>
<span class="line"><span style="color: #002339">im   </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Mode 1 interrupts</span></span>
<span class="line"><span style="color: #002339">ei                         </span><span style="color: #357B42; font-style: italic">; Activates interruptions</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleGameOver     </span><span style="color: #357B42; font-style: italic">; HL = title game over</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintMsg              </span><span style="color: #357B42; font-style: italic">; Paints the message</span></span>
<span class="line"><span style="color: #7EB233">endPlay_waitKey:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyAlpha          </span><span style="color: #357B42; font-style: italic">; Wait key</span></span>
<span class="line"><span style="color: #002339">cp   KEYN                  </span><span style="color: #357B42; font-style: italic">; Pressed N?</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   z, Menu               </span><span style="color: #357B42; font-style: italic">; Yes, menu</span></span>
<span class="line"><span style="color: #002339">cp   KEYn                  </span><span style="color: #357B42; font-style: italic">; Pushed n?</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   z, Menu               </span><span style="color: #357B42; font-style: italic">; Yes, menu</span></span>
<span class="line"><span style="color: #002339">cp   KEYS                  </span><span style="color: #357B42; font-style: italic">; Pulsed S?</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   z, Start              </span><span style="color: #357B42; font-style: italic">; Yes, start</span></span>
<span class="line"><span style="color: #002339">cp   KEYs                  </span><span style="color: #357B42; font-style: italic">; Pushed s?</span></span>
<span class="line"><span style="color: #7B30D0">jp</span><span style="color: #002339">   z, Start              </span><span style="color: #357B42; font-style: italic">; Yes, start</span></span>
<span class="line"><span style="color: #002339">jr   endPlay_waitKey       </span><span style="color: #357B42; font-style: italic">; Not pressed, loop</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We deactivate interruptions, switch to mode one, activate interruptions, wait for the question to be answered and, depending on the answer, we jump to one place or another.</p>



<ul class="wp-block-list">
<li class="has-medium-font-size">Menu: Return to the main menu. We can select the different options and enter new player names.</li>



<li class="has-medium-font-size">Start: is the label we have added. It clears the screen, paints the screen, resets the game data and switches interrupts to mode two.</li>
</ul>



<p class="has-medium-font-size wp-block-paragraph">If none of the expected keys have been pressed, it will loop until one is.</p>



<p class="has-medium-font-size wp-block-paragraph">If you go to the Menu tag, you will see that the first three lines are for switching to interrupt mode one, which is what we do in EndPlay. Delete the three lines below the Menu tag.</p>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and see the results. Everything seems to work fine, but at the end of the game the score is flashing. This is something we inherited from the beginning, although we didn&#8217;t notice it until now.</p>



<p class="has-medium-font-size wp-block-paragraph">To change the behaviour, locate PrintPoints in screen.asm and add the lines needed to disable the flickering just below it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   a,(ATTR_T)            ; A = temporary attributes
and  $7f                   ; Removes flicker 
ld   (ATTR_T), a           ; Update in memory" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   a,(ATTR_T)            </span><span style="color: #357B42; font-style: italic">; A = temporary attributes</span></span>
<span class="line"><span style="color: #7B30D0">and</span><span style="color: #002339">  </span><span style="color: #174781">$7f</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; Removes flicker </span></span>
<span class="line"><span style="color: #002339">ld   (ATTR_T), a           </span><span style="color: #357B42; font-style: italic">; Update in memory</span></span></code></pre></div>



<figure data-wp-context="{&quot;imageId&quot;:&quot;6a801ced0e765&quot;}" data-wp-interactive="core/image" data-wp-key="6a801ced0e765" class="wp-block-image aligncenter size-full wp-lightbox-container"><img data-recalc-dims="1" loading="lazy" decoding="async" width="702" height="606" data-attachment-id="4562" data-permalink="https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/tictactoe-s04-05/" data-orig-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-05.png?fit=702%2C606&amp;ssl=1" data-orig-size="702,606" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="TicTacToe-S04-05" data-image-description="" data-image-caption="&lt;p&gt;ZX Spectrum Assembly, Tic-Tac-Toe&lt;/p&gt;
" data-large-file="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-05.png?fit=702%2C606&amp;ssl=1" data-wp-class--hide="state.isContentHidden" data-wp-class--show="state.isContentVisible" data-wp-init="callbacks.setButtonStyles" data-wp-on--click="actions.showLightbox" data-wp-on--load="callbacks.setButtonStyles" data-wp-on--pointerdown="actions.preloadImage" data-wp-on--pointerenter="actions.preloadImageWithDelay" data-wp-on--pointerleave="actions.cancelPreload" data-wp-on-window--resize="callbacks.setButtonStyles" src="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-05.png?resize=702%2C606&#038;ssl=1" alt="ZX Spectrum Assembly, Tic-Tac-Toe" class="wp-image-4562" srcset="https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-05.png?w=702&amp;ssl=1 702w, https://i0.wp.com/espamatica.com/wp-content/uploads/2023/09/TicTacToe-S04-05.png?resize=300%2C259&amp;ssl=1 300w" sizes="auto, (max-width: 702px) 100vw, 702px" /><button
			class="lightbox-trigger"
			type="button"
			aria-haspopup="dialog"
			data-wp-bind--aria-label="state.thisImage.triggerButtonAriaLabel"
			data-wp-init="callbacks.initTriggerButton"
			data-wp-on--click="actions.showLightbox"
			data-wp-style--right="state.thisImage.buttonRight"
			data-wp-style--top="state.thisImage.buttonTop"
		>
			<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" fill="none" viewBox="0 0 12 12">
				<path fill="#fff" d="M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z" />
			</svg>
		</button><figcaption class="wp-element-caption">ZX Spectrum Assembly, Tic-Tac-Toe</figcaption></figure>



<p class="has-medium-font-size wp-block-paragraph">At this point, we can play the first full two-player games.</p>



<h2 id="zx-spectrum-assembly-tictactoe" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Tic-Tac-Toe</h2>



<p class="has-medium-font-size wp-block-paragraph">Want to play against the Spectrum and beat it? We will implement this in the next chapter of ZX Spectrum Assembly, </p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/28bdpg8p" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yprvqdhg" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<p class="wp-block-paragraph"></p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Tic-Tac-Toe by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309125309394" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309125309394/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x04-options/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4554</post-id>	</item>
		<item>
		<title>ZX Spectrum Assembly, Tic-Tac-Toe &#8211; 0x03 Sound</title>
		<link>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x03-sound/</link>
					<comments>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x03-sound/#respond</comments>
		
		<dc:creator><![CDATA[Juan Antonio Rubio García]]></dc:creator>
		<pubDate>Wed, 04 Oct 2023 07:00:00 +0000</pubDate>
				<category><![CDATA[ZX Spectrum]]></category>
		<category><![CDATA[Retro]]></category>
		<category><![CDATA[Z80 Assembly]]></category>
		<category><![CDATA[Assembly]]></category>
		<category><![CDATA[Tic-Tac_toe]]></category>
		<category><![CDATA[Z80]]></category>
		<guid isPermaLink="false">https://espamatica.com/?p=4521</guid>

					<description><![CDATA[In this chapter of&#160;ZX Spectrum Assembly,&#160;we will implement different sounds and use them for timing by pausing between events. Sound]]></description>
										<content:encoded><![CDATA[
<p class="has-medium-font-size wp-block-paragraph">In this chapter of&nbsp;<a href="https://espamatica.com/category/z80-assembly/">ZX Spectrum Assembly</a>,&nbsp;we will implement different sounds and use them for timing by pausing between events.</p>



<p class="wp-block-paragraph"><em><strong>Translation by <a href="https://www.playonretro.com/" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a></strong></em></p>


<h2 class="simpletoc-title">Table of contents</h2>
<ul class="simpletoc-list">
<li><a href="#sound">Sound</a>
</li>
<li><a href="#zx-spectrum-assembly-tictactoe">ZX Spectrum Assembly, Tic-Tac-Toe</a>
</li>
<li><a href="#useful-links">Useful links</a>
</li></ul>


<h2 id="sound" class="wp-block-heading has-large-font-size">Sound</h2>



<p class="has-medium-font-size wp-block-paragraph">We will create the file sound.asm and add the include to main.asm.</p>



<p class="has-medium-font-size wp-block-paragraph">In sound.asm we will add the definition of the different sounds and melodies.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Sounds.
;
; Sounds end in $00, the byte indicating the end.
; -------------------------------------------------------------------
; Countdown
SoundCountDown:
db $03, $8c, $3a, $00

; Error
SoundError:
db $0d, $c6, $1e, $16, $13, $13, $00

; Loss of movement
SoundLostMovement:	
db $0d, $07, $10, $0b, $96, $12, $0a, $4d, $14
db $0b, $96, $12, $1a, $2c, $08, $00

; Next player
SoundNextPlayer:
db $01, $9d, $7b, $00

; Spectrum Movement
SoundSpectrum:
db $06, $6e, $20, $06, $6e, $20, $05, $b7, $24
db $06, $6e, $20, $05, $13, $29, $05, $b7, $24
db $06, $6e, $20, $00

; Tables
SoundTie:
db $0d, $07, $10, $0b, $96, $12, $06, $d4, $1e
db $0b, $96, $12, $07, $a6, $1b, $0b, $96, $12
db $08, $a5, $18, $0b, $96, $12, $09, $b4, $15
db $0b, $96, $12, $0a, $4d, $14, $0b, $96, $12
db $0d, $07, $10, $00

; Point
SoundWinGame:
db $06, $6e, $20, $05, $13, $29, $04, $40, $30
db $06, $6e, $20, $05, $13, $29, $04, $40, $30
db $06, $6e, $20, $05, $13, $29, $04, $c7, $2b
db $05, $13, $29, $05, $b7, $24, $05, $13, $29
db $06, $6e, $20, $05, $13, $29, $04, $40, $30
db $06, $6e, $20, $05, $13, $29, $04, $40, $30
db $06, $6e, $20, $05, $13, $29, $04, $c7, $2b
db $05, $13, $29, $05, $b7, $24, $06, $6e, $20
db $00" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sounds.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Sounds end in $00, the byte indicating the end.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Countdown</span></span>
<span class="line"><span style="color: #7EB233">SoundCountDown:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$03</span><span style="color: #002339">, </span><span style="color: #174781">$8c</span><span style="color: #002339">, </span><span style="color: #174781">$3a</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Error</span></span>
<span class="line"><span style="color: #7EB233">SoundError:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0d</span><span style="color: #002339">, $c6, </span><span style="color: #174781">$1e</span><span style="color: #002339">, </span><span style="color: #174781">$16</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Loss of movement</span></span>
<span class="line"><span style="color: #7EB233">SoundLostMovement:</span><span style="color: #002339">	</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$0a</span><span style="color: #002339">, </span><span style="color: #174781">$4d</span><span style="color: #002339">, </span><span style="color: #174781">$14</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$1a</span><span style="color: #002339">, </span><span style="color: #174781">$2c</span><span style="color: #002339">, </span><span style="color: #174781">$08</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Next player</span></span>
<span class="line"><span style="color: #7EB233">SoundNextPlayer:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$01</span><span style="color: #002339">, </span><span style="color: #174781">$9d</span><span style="color: #002339">, </span><span style="color: #174781">$7b</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Spectrum Movement</span></span>
<span class="line"><span style="color: #7EB233">SoundSpectrum:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, $b7, </span><span style="color: #174781">$24</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, $b7, </span><span style="color: #174781">$24</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Tables</span></span>
<span class="line"><span style="color: #7EB233">SoundTie:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, $d4, </span><span style="color: #174781">$1e</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, $a6, </span><span style="color: #174781">$1b</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$08</span><span style="color: #002339">, $a5, </span><span style="color: #174781">$18</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$09</span><span style="color: #002339">, $b4, </span><span style="color: #174781">$15</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span><span style="color: #002339">, </span><span style="color: #174781">$0a</span><span style="color: #002339">, </span><span style="color: #174781">$4d</span><span style="color: #002339">, </span><span style="color: #174781">$14</span><span style="color: #002339">, </span><span style="color: #174781">$0b</span><span style="color: #002339">, </span><span style="color: #174781">$96</span><span style="color: #002339">, </span><span style="color: #174781">$12</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$0d</span><span style="color: #002339">, </span><span style="color: #174781">$07</span><span style="color: #002339">, </span><span style="color: #174781">$10</span><span style="color: #002339">, </span><span style="color: #174781">$00</span></span>
<span class="line"></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Point</span></span>
<span class="line"><span style="color: #7EB233">SoundWinGame:</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$40</span><span style="color: #002339">, </span><span style="color: #174781">$30</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$40</span><span style="color: #002339">, </span><span style="color: #174781">$30</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, $c7, </span><span style="color: #174781">$2b</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, $b7, </span><span style="color: #174781">$24</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$40</span><span style="color: #002339">, </span><span style="color: #174781">$30</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, </span><span style="color: #174781">$40</span><span style="color: #002339">, </span><span style="color: #174781">$30</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$04</span><span style="color: #002339">, $c7, </span><span style="color: #174781">$2b</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$05</span><span style="color: #002339">, </span><span style="color: #174781">$13</span><span style="color: #002339">, </span><span style="color: #174781">$29</span><span style="color: #002339">, </span><span style="color: #174781">$05</span><span style="color: #002339">, $b7, </span><span style="color: #174781">$24</span><span style="color: #002339">, </span><span style="color: #174781">$06</span><span style="color: #002339">, </span><span style="color: #174781">$6e</span><span style="color: #002339">, </span><span style="color: #174781">$20</span></span>
<span class="line"><span style="color: #0991B6">db</span><span style="color: #002339"> </span><span style="color: #174781">$00</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">All sounds have a zero byte as their last byte, which we will use in the routine that plays them to know when they are finished.</p>



<p class="has-medium-font-size wp-block-paragraph">In these definitions, each note is made up of three bytes instead of four, the first two being the note and the third being the low byte of the duration, all of which are sixteenths and/or half notes.</p>



<p class="has-medium-font-size wp-block-paragraph">We implement the routine that plays the tones.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="; -------------------------------------------------------------------
; Plays a melody or sound.
;
; Input: BC = Start address of the melody.
;
; To save is done byte by byte, with the duration always being fuzzy
; or half-fuse; only one byte is needed for the duration.
; This routine is adapted for this operation.
; -------------------------------------------------------------------
PlayMusic:
push af
push bc
push de
push hl
push ix                    ; Preserves records

playMusic_loop:
ld   a, (bc)               ; A = high byte note
ld   h, a                  ; H = A
or   a                     ; A = 0?
jr   z, playMusic_end      ; A = 0, end

inc  bc                    ; BC = next value
ld   a, (bc)               ; A = byte under note
ld   l, a                  ; L = A

inc  bc                    ; BC = next value
ld   a, (bc)               ; A = duration note
ld   e, a                  ; E = A
ld   d, $00                ; D = 0 (fuse or half-fuse)
inc  bc                    ; BC = next value

push bc                    ; Preserves BC
call BEEPER                ; Play note
pop  bc                    ; Recover BC

jr   playMusic_loop        ; Loop until it reaches 0 end melody

playMusic_end:
pop  ix
pop  hl
pop  de
pop  bc
pop  af                    ; Retrieves records
ret" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Plays a melody or sound.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; Input: BC = Start address of the melody.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">;</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; To save is done byte by byte, with the duration always being fuzzy</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; or half-fuse; only one byte is needed for the duration.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; This routine is adapted for this operation.</span></span>
<span class="line"><span style="color: #357B42; font-style: italic">; -------------------------------------------------------------------</span></span>
<span class="line"><span style="color: #7EB233">PlayMusic:</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> af</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> de</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> hl</span></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> ix                    </span><span style="color: #357B42; font-style: italic">; Preserves records</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">playMusic_loop:</span></span>
<span class="line"><span style="color: #002339">ld   a, (bc)               </span><span style="color: #357B42; font-style: italic">; A = high byte note</span></span>
<span class="line"><span style="color: #002339">ld   h, a                  </span><span style="color: #357B42; font-style: italic">; H = A</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; A = 0?</span></span>
<span class="line"><span style="color: #002339">jr   z, playMusic_end      </span><span style="color: #357B42; font-style: italic">; A = 0, end</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; BC = next value</span></span>
<span class="line"><span style="color: #002339">ld   a, (bc)               </span><span style="color: #357B42; font-style: italic">; A = byte under note</span></span>
<span class="line"><span style="color: #002339">ld   l, a                  </span><span style="color: #357B42; font-style: italic">; L = A</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; BC = next value</span></span>
<span class="line"><span style="color: #002339">ld   a, (bc)               </span><span style="color: #357B42; font-style: italic">; A = duration note</span></span>
<span class="line"><span style="color: #002339">ld   e, a                  </span><span style="color: #357B42; font-style: italic">; E = A</span></span>
<span class="line"><span style="color: #002339">ld   d, </span><span style="color: #174781">$00</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; D = 0 (fuse or half-fuse)</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; BC = next value</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">push</span><span style="color: #002339"> bc                    </span><span style="color: #357B42; font-style: italic">; Preserves BC</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> BEEPER                </span><span style="color: #357B42; font-style: italic">; Play note</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc                    </span><span style="color: #357B42; font-style: italic">; Recover BC</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">jr   playMusic_loop        </span><span style="color: #357B42; font-style: italic">; Loop until it reaches 0 end melody</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">playMusic_end:</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  ix</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  hl</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  de</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  bc</span></span>
<span class="line"><span style="color: #7B30D0">pop</span><span style="color: #002339">  af                    </span><span style="color: #357B42; font-style: italic">; Retrieves records</span></span>
<span class="line"><span style="color: #7B30D0">ret</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">PlayMusic receives the address of the sound to be played in BC, loads the note in HL, the duration in DE, plays it and loops until it reaches the zero byte which marks the end of the sound. D is always zero, only one byte is used for the duration.</p>



<p class="has-medium-font-size wp-block-paragraph">In main.asm we will add the necessary calls to play the sounds.</p>



<p class="has-medium-font-size wp-block-paragraph">We locate the Loop tag and delete from LD B, $19 to DJNZ loop_wait as we will be timing with the sound. Below this, after CALL PrintPoints, we add the next player sound:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   bc, SoundNextPlayer
call PlayMusic" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   bc, SoundNextPlayer</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate the loop_print tag and just above it the JR Loop line. Above this line we add the error sound:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   bc, SoundError
call PlayMusic" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   bc, SoundError</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate the loop_win tag and just below it the INC (HL) line. Below this line we add the dot winner sound:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   bc, SoundWinGame
call PlayMusic" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   bc, SoundWinGame</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">Locate the loop_reset tag and just above it add the table sound:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="ld   bc, SoundTie
call PlayMusic" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">ld   bc, SoundTie</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic</span></span></code></pre></div>



<p class="has-medium-font-size wp-block-paragraph">We compile, load into the emulator and listen to every single sound.</p>



<p class="has-medium-font-size wp-block-paragraph">The main.asm file has grown considerably since we started, so it&#8217;s time to see what it looks like after commenting.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:clamp(16px, 1rem, 24px);font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#002339;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:clamp(16px, 1rem, 24px);--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#003353">ASM</span><span role="button" tabindex="0" data-code="org  $5e88                 ; Loading address

Main:
ld   hl, Sprite_P1         ; HL = address Sprite_P1
ld   (UDG), hl             ; UDG = address Sprite_p1
ld   a, $02                ; A = 2
call OPENCHAN              ; Open channel 2

xor  a                     ; A = 0, Z = 0, C = 0
call BORDER                ; Change border
call CLA                   ; Changes display attributes
call CLS                   ; Clear screen

Init:
call PrintBoard            ; Paint board

ld   hl, Name_p1           ; HL = name player 1
ld   de, player1_name      ; DE = player1_name
ld   bc, LENNAME           ; BC = length name
ldir                       ; Pass data
ld   hl, Name_p2           ; HL = name player 2
ld   de, player2_name      ; DE = player 2 name
ld   bc, LENNAME           ; BC = length name
ldir                       ; Pass data
call PrintInfo             ; Paint info

ld   bc, LENDATA           ; BC = length of starting data
call ResetValues           ; Clear data

Loop:
ld   hl, TitleTurn         ; HL = shift address
ld   de, TitleTurn_name    ; DE = address name in turn name in turn
call DoMsg                 ; Compose and paint shift
call PrintPoints           ; PaintPoints
ld   bc, SoundNextPlayer   ; BC = sound direction
call PlayMusic             ; Play sound
loop_key:
call WaitKeyBoard          ; Wait for key press
ld   a, c                  ; A = C
cp   KEY0                  ; Key pressed?
jr   z, loop_key           ; No, loop
call ToMove                ; Check movement
jr   z, loop_print         ; Correct, skip
ld   hl, TitleError        ; HL = address error
call PrintMsg              ; Paint Error
ld   bc, SoundError        ; BC = sound address
call PlayMusic             ; Play sound
jr   Loop                  ; Main loop
loop_print:
call PrintOXO              ; Paint token
loop_checkWinner:
call CheckWinner           ; Any winners?
jr   nz, loop_tie          ; No, check tables
call PrintWinnerLine       ; Paints winning line
ld   hl, TitlePointFor     ; HL = winning address
ld   de, TitlePointName    ; HL = address winning name
call DoMsg                 ; Compose and paint winner
ld   hl, Points_p1         ; HL = address points player 1
ld   a, (PlayerMoves)      ; A = player that moved
or   a                     ; Player 1?
jr   z, loop_win           ; Yes, skip
inc  hl                    ; HL = address points player 2
loop_win:
inc  (hl)                  ; Increases score
ld   bc, SoundWinGame      ; BC = sound address
call PlayMusic             ; Play sound
jr   loop_reset            ; Jump
loop_tie:
ld   hl, MoveCounter       ; HL = move counter address
inc  (hl)                  ; Increments counter
ld   a, $09                ; A = 9
cp   (hl)                  ; Counter = 9?
jr   nz, loop_cont         ; No, skip
ld   hl, Points_tie        ; HL = address points tables
inc  (hl)                  ; Increases score
ld   hl, TitleTie          ; HL = address tables
call PrintMsg              ; Paint tables
ld   bc, SoundTie          ; BC = sound direction
call PlayMusic             ; Play sound
loop_reset:
ld   bc, LENDATA-$04       ; BC = data length clean data
call ResetValues           ; Clear data
call PrintBoard            ; Paint board
loop_cont:
ld   a, (PlayerMoves)      ; A = player who moved
xor  $01                   ; A = next player
ld   (PlayerMoves), a      ; Refresh in memory
jr   Loop                  ; Main loop

include &quot;game.asm&quot;
include &quot;rom.asm&quot;
include &quot;screen.asm&quot;
include &quot;sprite.asm&quot;
include &quot;sound.asm&quot;
include &quot;var.asm&quot;

end  Main" style="color:#002339;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6"></path></svg></span><pre class="shiki slack-ochin" style="background-color: #FFF" tabindex="0"><code><span class="line"><span style="color: #002339">org  </span><span style="color: #174781">$5e88</span><span style="color: #002339">                 </span><span style="color: #357B42; font-style: italic">; Loading address</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">Main:</span></span>
<span class="line"><span style="color: #002339">ld   hl, Sprite_P1         </span><span style="color: #357B42; font-style: italic">; HL = address Sprite_P1</span></span>
<span class="line"><span style="color: #002339">ld   (UDG), hl             </span><span style="color: #357B42; font-style: italic">; UDG = address Sprite_p1</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$02</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 2</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> OPENCHAN              </span><span style="color: #357B42; font-style: italic">; Open channel 2</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  a                     </span><span style="color: #357B42; font-style: italic">; A = 0, Z = 0, C = 0</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> BORDER                </span><span style="color: #357B42; font-style: italic">; Change border</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLA                   </span><span style="color: #357B42; font-style: italic">; Changes display attributes</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CLS                   </span><span style="color: #357B42; font-style: italic">; Clear screen</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7EB233">Init:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBoard            </span><span style="color: #357B42; font-style: italic">; Paint board</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p1           </span><span style="color: #357B42; font-style: italic">; HL = name player 1</span></span>
<span class="line"><span style="color: #002339">ld   de, player1_name      </span><span style="color: #357B42; font-style: italic">; DE = player1_name</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME           </span><span style="color: #357B42; font-style: italic">; BC = length name</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Pass data</span></span>
<span class="line"><span style="color: #002339">ld   hl, Name_p2           </span><span style="color: #357B42; font-style: italic">; HL = name player 2</span></span>
<span class="line"><span style="color: #002339">ld   de, player2_name      </span><span style="color: #357B42; font-style: italic">; DE = player 2 name</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENNAME           </span><span style="color: #357B42; font-style: italic">; BC = length name</span></span>
<span class="line"><span style="color: #002339">ldir                       </span><span style="color: #357B42; font-style: italic">; Pass data</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintInfo             </span><span style="color: #357B42; font-style: italic">; Paint info</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">ld   bc, LENDATA           </span><span style="color: #357B42; font-style: italic">; BC = length of starting data</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ResetValues           </span><span style="color: #357B42; font-style: italic">; Clear data</span></span>
<span class="line"></span>
<span class="line"><span style="color: #7B30D0">Loop</span><span style="color: #002339">:</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleTurn         </span><span style="color: #357B42; font-style: italic">; HL = shift address</span></span>
<span class="line"><span style="color: #002339">ld   de, TitleTurn_name    </span><span style="color: #357B42; font-style: italic">; DE = address name in turn name in turn</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> DoMsg                 </span><span style="color: #357B42; font-style: italic">; Compose and paint shift</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintPoints           </span><span style="color: #357B42; font-style: italic">; PaintPoints</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundNextPlayer   </span><span style="color: #357B42; font-style: italic">; BC = sound direction</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Play sound</span></span>
<span class="line"><span style="color: #7EB233">loop_key:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> WaitKeyBoard          </span><span style="color: #357B42; font-style: italic">; Wait for key press</span></span>
<span class="line"><span style="color: #002339">ld   a, c                  </span><span style="color: #357B42; font-style: italic">; A = C</span></span>
<span class="line"><span style="color: #002339">cp   KEY0                  </span><span style="color: #357B42; font-style: italic">; Key pressed?</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_key           </span><span style="color: #357B42; font-style: italic">; No, loop</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ToMove                </span><span style="color: #357B42; font-style: italic">; Check movement</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_print         </span><span style="color: #357B42; font-style: italic">; Correct, skip</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleError        </span><span style="color: #357B42; font-style: italic">; HL = address error</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintMsg              </span><span style="color: #357B42; font-style: italic">; Paint Error</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundError        </span><span style="color: #357B42; font-style: italic">; BC = sound address</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Play sound</span></span>
<span class="line"><span style="color: #002339">jr   </span><span style="color: #7B30D0">Loop</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Main loop</span></span>
<span class="line"><span style="color: #7EB233">loop_print:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintOXO              </span><span style="color: #357B42; font-style: italic">; Paint token</span></span>
<span class="line"><span style="color: #7EB233">loop_checkWinner:</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> CheckWinner           </span><span style="color: #357B42; font-style: italic">; Any winners?</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_tie          </span><span style="color: #357B42; font-style: italic">; No, check tables</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintWinnerLine       </span><span style="color: #357B42; font-style: italic">; Paints winning line</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitlePointFor     </span><span style="color: #357B42; font-style: italic">; HL = winning address</span></span>
<span class="line"><span style="color: #002339">ld   de, TitlePointName    </span><span style="color: #357B42; font-style: italic">; HL = address winning name</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> DoMsg                 </span><span style="color: #357B42; font-style: italic">; Compose and paint winner</span></span>
<span class="line"><span style="color: #002339">ld   hl, Points_p1         </span><span style="color: #357B42; font-style: italic">; HL = address points player 1</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)      </span><span style="color: #357B42; font-style: italic">; A = player that moved</span></span>
<span class="line"><span style="color: #7B30D0">or</span><span style="color: #002339">   a                     </span><span style="color: #357B42; font-style: italic">; Player 1?</span></span>
<span class="line"><span style="color: #002339">jr   z, loop_win           </span><span style="color: #357B42; font-style: italic">; Yes, skip</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  hl                    </span><span style="color: #357B42; font-style: italic">; HL = address points player 2</span></span>
<span class="line"><span style="color: #7EB233">loop_win:</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)                  </span><span style="color: #357B42; font-style: italic">; Increases score</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundWinGame      </span><span style="color: #357B42; font-style: italic">; BC = sound address</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Play sound</span></span>
<span class="line"><span style="color: #002339">jr   loop_reset            </span><span style="color: #357B42; font-style: italic">; Jump</span></span>
<span class="line"><span style="color: #7EB233">loop_tie:</span></span>
<span class="line"><span style="color: #002339">ld   hl, MoveCounter       </span><span style="color: #357B42; font-style: italic">; HL = move counter address</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)                  </span><span style="color: #357B42; font-style: italic">; Increments counter</span></span>
<span class="line"><span style="color: #002339">ld   a, </span><span style="color: #174781">$09</span><span style="color: #002339">                </span><span style="color: #357B42; font-style: italic">; A = 9</span></span>
<span class="line"><span style="color: #002339">cp   (hl)                  </span><span style="color: #357B42; font-style: italic">; Counter = 9?</span></span>
<span class="line"><span style="color: #002339">jr   nz, loop_cont         </span><span style="color: #357B42; font-style: italic">; No, skip</span></span>
<span class="line"><span style="color: #002339">ld   hl, Points_tie        </span><span style="color: #357B42; font-style: italic">; HL = address points tables</span></span>
<span class="line"><span style="color: #7B30D0">inc</span><span style="color: #002339">  (hl)                  </span><span style="color: #357B42; font-style: italic">; Increases score</span></span>
<span class="line"><span style="color: #002339">ld   hl, TitleTie          </span><span style="color: #357B42; font-style: italic">; HL = address tables</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintMsg              </span><span style="color: #357B42; font-style: italic">; Paint tables</span></span>
<span class="line"><span style="color: #002339">ld   bc, SoundTie          </span><span style="color: #357B42; font-style: italic">; BC = sound direction</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PlayMusic             </span><span style="color: #357B42; font-style: italic">; Play sound</span></span>
<span class="line"><span style="color: #7EB233">loop_reset:</span></span>
<span class="line"><span style="color: #002339">ld   bc, LENDATA-</span><span style="color: #174781">$04</span><span style="color: #002339">       </span><span style="color: #357B42; font-style: italic">; BC = data length clean data</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> ResetValues           </span><span style="color: #357B42; font-style: italic">; Clear data</span></span>
<span class="line"><span style="color: #7B30D0">call</span><span style="color: #002339"> PrintBoard            </span><span style="color: #357B42; font-style: italic">; Paint board</span></span>
<span class="line"><span style="color: #7EB233">loop_cont:</span></span>
<span class="line"><span style="color: #002339">ld   a, (PlayerMoves)      </span><span style="color: #357B42; font-style: italic">; A = player who moved</span></span>
<span class="line"><span style="color: #7B30D0">xor</span><span style="color: #002339">  </span><span style="color: #174781">$01</span><span style="color: #002339">                   </span><span style="color: #357B42; font-style: italic">; A = next player</span></span>
<span class="line"><span style="color: #002339">ld   (PlayerMoves), a      </span><span style="color: #357B42; font-style: italic">; Refresh in memory</span></span>
<span class="line"><span style="color: #002339">jr   </span><span style="color: #7B30D0">Loop</span><span style="color: #002339">                  </span><span style="color: #357B42; font-style: italic">; Main loop</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">include &quot;game.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;rom.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;screen.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;sprite.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;sound.asm&quot;</span></span>
<span class="line"><span style="color: #002339">include &quot;var.asm&quot;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #002339">end  Main</span></span></code></pre></div>



<h2 id="zx-spectrum-assembly-tictactoe" class="wp-block-heading has-large-font-size">ZX Spectrum Assembly, Tic-Tac-Toe</h2>



<p class="has-medium-font-size wp-block-paragraph">We have a large part of the game developed and working. We can play our first games with friends and/or family and we have sound.</p>



<p class="has-medium-font-size wp-block-paragraph">In the next chapter of ZX Spectrum Assembly, we will implement the different options of the game and the end of the game.</p>



<p class="has-medium-font-size wp-block-paragraph">Download the source code from&nbsp;<a href="https://tinyurl.com/283e38v7" target="_blank" rel="noreferrer noopener nofollow">here</a>.</p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<h2 class="wp-block-heading has-large-font-size">Useful links</h2>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://notepad-plus-plus.org/" target="_blank" rel="noreferrer noopener nofollow">Notepad++</a></li>



<li class="has-medium-font-size"><a href="https://code.visualstudio.com/" target="_blank" rel="noreferrer noopener nofollow">Visual Studio Code</a></li>



<li class="has-medium-font-size"><a href="https://www.sublimetext.com/" target="_blank" rel="noreferrer noopener nofollow">Sublime Text</a></li>



<li class="has-medium-font-size"><a href="https://github.com/chernandezba/zesarux" target="_blank" rel="noreferrer noopener nofollow">ZEsarUX</a></li>



<li class="has-medium-font-size"><a href="https://pasmo.speccy.org/" target="_blank" rel="noreferrer noopener nofollow">Pasmo</a></li>



<li class="has-medium-font-size"><a href="https://wiki.speccy.org/cursos/ensamblador/indice" target="_blank" rel="noreferrer noopener nofollow">Compiler Software Z80 Assembly Course</a></li>



<li class="has-medium-font-size"><a href="https://git-scm.com/book/en/v2/Inicio---Sobre-el-Control-de-Versiones-Instalaci%C3%B3n-de-Git" target="_blank" rel="noreferrer noopener nofollow">Git</a></li>



<li class="has-medium-font-size"><a href="https://t.me/EnsambladorZXSpectrum" target="_blank" rel="noreferrer noopener nofollow">Z80 Assembly on Telegram</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/27fbuyfb" target="_blank" rel="noreferrer noopener nofollow">Z80 Reference</a></li>



<li class="has-medium-font-size"><a href="https://tinyurl.com/yprvqdhg" target="_blank" rel="noreferrer noopener nofollow">Complete tutorial in PDF format</a></li>
</ul>
</div>



<p class="wp-block-paragraph"></p>



<div class="wp-block-group is-layout-constrained wp-block-group-is-layout-constrained">
<p class="has-cyan-bluish-gray-background-color has-background has-medium-font-size wp-block-paragraph">ZX Spectrum Assembly, Tic-Tac-Toe by&nbsp;<a href="https://espamatica.com/">Juan Antonio Rubio García</a>.<br>Translation by&nbsp;<a href="https://twitter.com/PlayOnRetro" target="_blank" rel="noreferrer noopener nofollow">Felipe Monge Corbalán</a>.<br>This work is licensed to&nbsp;<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noreferrer noopener nofollow">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a>.<br>Any comments are always welcome.</p>



<figure class="wp-block-image"><a href="http://www.safecreative.org/work/2309125309394" target="_blank" rel="noreferrer noopener nofollow"><img decoding="async" src="https://resources.safecreative.org/work-creativity-declaration/2309125309394/label/declaration-1-human-real-300" alt="" title="null"/></a></figure>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://espamatica.com/zx-spectrum-assembly-tic-tac-toe-0x03-sound/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4521</post-id>	</item>
	</channel>
</rss>
