home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1996 December / PC_Shareware-1996-12.iso / windows / spectrum / sources / video.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-26  |  4.8 KB  |  190 lines

  1.  
  2. /* Video.c: Translates hardware screen addresses to coord
  3.  *     system, fill screen buffer and implements Flash.
  4.  * 
  5.  * Copyright 1996 Rui Fernando Ferreira Ribeiro.
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include "env.h"
  25.  
  26.  
  27. /* buffer caching the Spectrum attributes state */
  28. static char attrib_z80[24][32];
  29.  
  30. //  Write a byte on Spectrum memory, at the attribute cache,
  31. // and at the WinG buffer if needed
  32. void writebyte(unsigned short adress, unsigned char byte)
  33. {
  34.    unsigned char i;
  35.    UCHAR x, y;      /* coordenadas */
  36.    static unsigned char colour = 0;  /* ultimo atributo   */
  37.    static unsigned char ink = 0;     /* ink   (com flash) */
  38.    static unsigned char paper = 0;   /* paper (com flash) */
  39.  
  40.    /* each memory acess = 3T */
  41.  
  42.    /* if ROM defined can't write in lower adresses
  43.     */
  44.    if(adress < 0x4000)
  45.       return; 
  46.  
  47.    *(mem + adress) = byte;
  48.  
  49.  
  50.    if(adress < 0x5800) /* If adress lower than attributes adress */
  51.    {
  52.       static ladress = 0x4000;
  53.       static lbyte = 0;
  54.  
  55.       WindowDirty = 1;
  56.       if((ladress != adress) || (adress == 0x4000))
  57.       { 
  58.          /* put adress in univ x,y coords and read attribs corresponding with
  59.        that coords i.e. x=0,y=0 ecran top
  60.           */
  61.          y = ((ladress >> 8) & 7) | ((ladress >> 2) & 0x38) |
  62.                ((ladress >> 5) & 0xc0);
  63.       /* if attrib different recalculates ink&paper */
  64.  
  65.       if(colour != attrib_z80[y>>3][x = (ladress & 0x1F)])
  66.       {
  67.          colour = attrib_z80[y>>3][x];
  68.      paper = (colour >> 3) & 0xF;
  69.      ink = (colour & 7) | ((colour >> 3) & 8);
  70.       }
  71.  
  72.       x <<= 3;
  73.       /* put in host 8 pixels corresponding to Spectrum pixels
  74.        */
  75.       for(i = 0 ; i < 8 ; i++)
  76.       {
  77.          pixel_host(x++, y, (lbyte & 0x80)?ink:paper);
  78.      lbyte <<= 1;
  79.       }
  80.       ladress = adress;
  81.       }
  82.       lbyte = byte;
  83.    }
  84.    else
  85.       if(adress < 0x5B00) /* If adress in attrib zone */
  86.       {
  87.          unsigned char k;
  88.  
  89.      WindowDirty = 1;
  90.      /* if attrib changed */
  91.      if(attrib_z80[y = (adress >> 5) & 0x1f][x = adress & 0x1f] != byte)
  92.      {
  93.         /* keep it
  94.          */
  95.         colour = attrib_z80[y][x] = byte;
  96.         /* recalculate ink&paper
  97.          */
  98.         paper = (colour >> 3) & 0xF;
  99.         ink = (colour & 7) | ((colour >> 3) & 8);
  100.  
  101.         /* Transform text coords in screen adress
  102.          */
  103.         adress = ((y & 7)<<5) | ((y & 0x18)<<8) | x | 0x4000;
  104.  
  105.         /* put ∩text∩ coords in graphic coords
  106.          */
  107.         y <<= 3;
  108.         x <<= 3;
  109.  
  110.         /* Print corresponding attribut square (8 * 8 pixels)
  111.          */
  112.         for(k = 0 ; k < 8 ; k++)
  113.         {
  114.            byte = readbyte(adress);
  115.            for(i = 0 ; i < 8 ; i++)
  116.            {
  117.               pixel_host(x++, y, (byte & 0x80)?ink:paper);
  118.           byte <<= 1;
  119.            }
  120.            /* go to next scan
  121.             */
  122.            adress += 256;
  123.            x -= 8;
  124.            y++;
  125.         }
  126.      }
  127.       }
  128. }
  129.  
  130.  
  131. // Do the real work for flash happening on the WinG buffer
  132. void do_flash()
  133. {
  134.    UCHAR colour, x1, y1, x, y, k, paper, ink, byte, i;
  135.    USHORT adress;
  136.  
  137.    // hack for flushing byte buffer
  138.    writebyte(0x4000, readbyte(0x4000) );
  139.  
  140.    for(y1 = 0 ; y1 < 24 ; y1++)
  141.    {
  142.       for(x1 = 0 ; x1 < 32 ; x1++)
  143.       {
  144.      x = x1;
  145.      y = y1;
  146.      if((colour = attrib_z80[y][x]) & 0x80)
  147.      {
  148.         if(!FlashState)
  149.             {
  150.            paper = (colour >> 3) & 0xF;
  151.            ink = (colour & 7) | ((colour >> 3) & 8);
  152.         }
  153.         else
  154.         {
  155.            ink   = (colour >> 3) & 0xF;
  156.            paper = (colour & 7) | ((colour >> 3) & 8);
  157.             }
  158.  
  159.         /* Transform text coords in screen adress
  160.          */
  161.         adress = ((y & 7)<<5) | ((y & 0x18)<<8) | x | 0x4000;
  162.  
  163.         /* put ∩text∩ coords in graphic coords
  164.          */
  165.         y <<= 3;
  166.         x <<= 3;
  167.  
  168.         /* Print corresponding attribut square (8 * 8 pixels)
  169.          */
  170.         for(k = 0 ; k < 8 ; k++)
  171.         {
  172.            byte = readbyte(adress);
  173.            for(i = 0 ; i < 8 ; i++)
  174.            {
  175.               pixel_host(x++, y, (byte & 0x80)?ink:paper);
  176.           byte <<= 1;
  177.            }
  178.            /* go to next scan
  179.             */
  180.            adress += 256;
  181.            x -= 8;
  182.            y++;
  183.         }
  184.          }
  185.       }     
  186.    }
  187. }
  188.  
  189. /* Video.c */
  190.