Skip to content

Instantly share code, notes, and snippets.

@tschak909
Created August 1, 2018 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tschak909/ad4542db2114c34c279c3a6d3fb03776 to your computer and use it in GitHub Desktop.
Save tschak909/ad4542db2114c34c279c3a6d3fb03776 to your computer and use it in GitHub Desktop.
terminal_char_load current implementation.
/**
* PLATOTerm64 - A PLATO Terminal for the Commodore 64
* Based on Steve Peltz's PAD
*
* Author: Thomas Cherryhomes <thom.cherryhomes at gmail dot com>
*
* terminal_char_load.c - Character set loading routine for 5x6 font.
* Thanks to Michael Sternberg (16kRAM/atari) for fixing Algorithm B
*/
#include <string.h>
#include <stdarg.h>
#include "../terminal.h"
#include "../protocol.h"
// Temporary PLATO character data, 8x16 matrix
static unsigned char char_data[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
static unsigned char BTAB[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; // flip one bit on (OR)
static unsigned char BTAB_5[]={0x08,0x10,0x10,0x20,0x20,0x40,0x80,0x80}; // flip one bit on for the 5x6 matrix (OR)
static unsigned char TAB_0_5[]={0x00,0x00,0x00,0x01,0x01,0x01,0x02,0x02,0x03,0x03,0x04,0x04,0x04,0x05,0x05,0x05};
static unsigned char TAB_0_4[]={0x00,0x00,0x01,0x02,0x02,0x03,0x03,0x04}; // return 0..4 given index 0 to 7
static unsigned char PIX_THRESH[]={0x03,0x02,0x03,0x03,0x02, // Pixel threshold table.
0x03,0x02,0x03,0x03,0x02,
0x02,0x01,0x02,0x02,0x01,
0x02,0x01,0x02,0x02,0x01,
0x03,0x02,0x03,0x03,0x02,
0x03,0x02,0x03,0x03,0x02};
static unsigned char PIX_WEIGHTS[]={0x00,0x00,0x00,0x00,0x00, // Pixel weights
0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00};
static unsigned char TAB_0_25[]={0,5,10,15,20,25}; // Given index 0 of 5, return multiple of 5.
static unsigned char pix_cnt; // total # of pixels
static unsigned char curr_word; // current word
static unsigned char u,v; // loop counters
static unsigned char temp_byte; // Temporary byte.
// Added by Michael Sternberg (16kRAM / atari)
// Had to change to hex, because CC65 does not do binary notation.
static unsigned char ROR8[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
static unsigned int ROL16[] = {0x8000, 0x4000, 0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100, 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
extern unsigned char fontm23[768];
extern unsigned short fontptr[160];
/**
* terminal_char_load - Store a character into the user definable
* character set.
*/
void terminal_char_load(padWord charnum, charData theChar)
{
// Clear char data.
memset(char_data,0,sizeof(char_data));
memset(PIX_WEIGHTS,0,sizeof(PIX_WEIGHTS));
memset(&fontm23[fontptr[charnum]],0,6);
// Transpose character data.
for (curr_word=0;curr_word<8;curr_word++)
{
for (u=16; u-->0; )
{
if (theChar[curr_word] & ROL16[u])
{
pix_cnt++;
PIX_WEIGHTS[TAB_0_25[TAB_0_5[u]]+TAB_0_4[curr_word]]++;
char_data[u]|=BTAB[curr_word];
}
}
}
// Determine algorithm to use for number of pixels.
// Algorithm A is used when roughly half of the # of pixels are set.
// Algorithm B is used either when the image is densely or sparsely populated (based on pix_cnt).
if ((54 <= pix_cnt) && (pix_cnt < 85))
{
// Algorithm A - approx Half of pixels are set
for (u=6; u-->0; )
{
for (v=5; v-->0; )
{
if (PIX_WEIGHTS[TAB_0_25[u]+v] >= PIX_THRESH[TAB_0_25[u]+v])
fontm23[fontptr[charnum]+u]|=BTAB[v];
}
}
}
else if ((pix_cnt < 54) || (pix_cnt >= 85))
{
// Algorithm B - Sparsely or heavily populated bitmaps
for (u=16; u-->0; )
{
// more than 85 pixels? invert the bitmap. -Fixed by Michael Sternberg (16kRAM/atari)
if (pix_cnt >= 85)
char_data[u] ^= 0xFF;
temp_byte=0x00;
for (v=8; v-->0; )
{
if (char_data[u] & ROR8[v])
temp_byte|=BTAB_5[v];
fontm23[fontptr[charnum]+TAB_0_5[u]]|=temp_byte;
}
}
if (pix_cnt >= 85)
{
for (v=6; v-->0; )
{
fontm23[fontptr[charnum]+v] ^= 0xFF;
fontm23[fontptr[charnum]+v] &= 0xF8;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment