<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "common.h"
#include "cpu.h"
#include "mfp.h"
#include "screen.h"
#include "mmu.h"

#define PPMOUTPUT 0

#define SHIFTERSIZE 256
#define SHIFTERBASE 0xff8200

#define HBLSIZE 512
#define HBLPRE 64
#define HBLSCR 320
#define HBLPOST 88

#define VBLSIZE 313
#define VBLPRE 64
#define VBLSCR 200
#define VBLPOST 40

#define SCR_BYTES_PER_LINE 160

#define BORDERTOP 28
#define BORDERBOTTOM 38
#define BORDERLEFT 32
#define BORDERRIGHT 32


static long linenum = 0;
static long linecnt = HBLSIZE+432; /* Cycle count per line, for timer-b event */
static long vsynccnt = VBLSIZE*HBLSIZE; /* 160256 cycles in one screen */
static long hsynccnt = HBLSIZE+16; /* Offset for first hbl interrupt */
static long lastrasterpos = 0;
static long lastcpucnt;

static long palette_r[16*2]; /* x2 for slightly different border col */
static long palette_g[16*2]; /* x2 for slightly different border col */
static long palette_b[16*2]; /* x2 for slightly different border col */

static WORD stpal[16];
static LONG curaddr; /* Current address used for display */
static LONG scraddr; /* Address for next VBL */
static LONG scrptr;  /* Actual screen pointer, read only from emulator */
static BYTE syncreg; /* 50/60Hz + Internal/External Sync flags */
static BYTE resolution; /* Low, medium or high resolution. Only low now. */

static int vblpre = VBLPRE;
static int vblscr = VBLSCR;
static int hblpre = HBLPRE;
static int hblscr = HBLSCR;
static int scr_bytes_per_line = SCR_BYTES_PER_LINE;

static int ppm_fd;
static unsigned char *rgbimage;

static void set_palette(int pnum, int value, int part)
{
  int c;

  switch(part) {
  case 1: /* Low byte, only Red */
    c = (value&0x7)<<5;
    palette_r[pnum] = c;
    palette_r[pnum+16] = c|0x1f; /* Brighter for border */
    break;
  case 2: /* High byte, only Green and Blue */
    c = (value&0x7)<<5;
    palette_b[pnum] = c;
    palette_b[pnum+16] = c|0x1f; /* Brighter for border */
    c = (value&0x70)<<1;
    palette_g[pnum] = c;
    palette_g[pnum+16] = c|0x1f; /* Brighter for border */
    break;
  default: /* Currently unused */
    break;
  }
}

static void set_pixel(int rasterpos, int pnum)
{

  //  if(pnum == 0) return;

  rgbimage[rasterpos*3+0] = palette_r[pnum];
  rgbimage[rasterpos*3+1] = palette_g[pnum];
  rgbimage[rasterpos*3+2] = palette_b[pnum];
}

static void fill_16pxl(int rasterpos, int pnum)
{
  int i;

  for(i=0;i<16;i++) {
    set_pixel(rasterpos+i, pnum);
  }
}

static int get_pixel(int videooffset, int pxlnum)
{
  int c;
  WORD d[4];

  d[3] = mmu_read_word_print(curaddr+videooffset*2+0);
  d[2] = mmu_read_word_print(curaddr+videooffset*2+2);
  d[1] = mmu_read_word_print(curaddr+videooffset*2+4);
  d[0] = mmu_read_word_print(curaddr+videooffset*2+6);
  
  c = ((((d[0]>>(15-pxlnum))&1)<<3)|
       (((d[1]>>(15-pxlnum))&1)<<2)|
       (((d[2]>>(15-pxlnum))&1)<<1)|
       (((d[3]>>(15-pxlnum))&1)));
  return c;<commit_msg>Significant speedup of emulator by caching last block of 16 pixels when building display<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "common.h"
#include "cpu.h"
#include "mfp.h"
#include "screen.h"
#include "mmu.h"

#define PPMOUTPUT 0

#define SHIFTERSIZE 256
#define SHIFTERBASE 0xff8200

#define HBLSIZE 512
#define HBLPRE 64
#define HBLSCR 320
#define HBLPOST 88

#define VBLSIZE 313
#define VBLPRE 64
#define VBLSCR 200
#define VBLPOST 40

#define SCR_BYTES_PER_LINE 160

#define BORDERTOP 28
#define BORDERBOTTOM 38
#define BORDERLEFT 32
#define BORDERRIGHT 32


static long linenum = 0;
static long linecnt = HBLSIZE+432; /* Cycle count per line, for timer-b event */
static long vsynccnt = VBLSIZE*HBLSIZE; /* 160256 cycles in one screen */
static long hsynccnt = HBLSIZE+16; /* Offset for first hbl interrupt */
static long lastrasterpos = 0;
static long lastcpucnt;

static long palette_r[16*2]; /* x2 for slightly different border col */
static long palette_g[16*2]; /* x2 for slightly different border col */
static long palette_b[16*2]; /* x2 for slightly different border col */

static WORD stpal[16];
static LONG curaddr; /* Current address used for display */
static LONG scraddr; /* Address for next VBL */
static LONG scrptr;  /* Actual screen pointer, read only from emulator */
static BYTE syncreg; /* 50/60Hz + Internal/External Sync flags */
static BYTE resolution; /* Low, medium or high resolution. Only low now. */

static int vblpre = VBLPRE;
static int vblscr = VBLSCR;
static int hblpre = HBLPRE;
static int hblscr = HBLSCR;
static int scr_bytes_per_line = SCR_BYTES_PER_LINE;

static int ppm_fd;
static unsigned char *rgbimage;

static void set_palette(int pnum, int value, int part)
{
  int c;

  switch(part) {
  case 1: /* Low byte, only Red */
    c = (value&0x7)<<5;
    palette_r[pnum] = c;
    palette_r[pnum+16] = c|0x1f; /* Brighter for border */
    break;
  case 2: /* High byte, only Green and Blue */
    c = (value&0x7)<<5;
    palette_b[pnum] = c;
    palette_b[pnum+16] = c|0x1f; /* Brighter for border */
    c = (value&0x70)<<1;
    palette_g[pnum] = c;
    palette_g[pnum+16] = c|0x1f; /* Brighter for border */
    break;
  default: /* Currently unused */
    break;
  }
}

static void set_pixel(int rasterpos, int pnum)
{

  //  if(pnum == 0) return;

  rgbimage[rasterpos*3+0] = palette_r[pnum];
  rgbimage[rasterpos*3+1] = palette_g[pnum];
  rgbimage[rasterpos*3+2] = palette_b[pnum];
}

static void fill_16pxl(int rasterpos, int pnum)
{
  int i;

  for(i=0;i<16;i++) {
    set_pixel(rasterpos+i, pnum);
  }
}

static int get_pixel(int videooffset, int pxlnum)
{
  int c;
  static int lastpos = 0;
  static WORD d[4];

  if((curaddr+videooffset*2) != lastpos) {
    d[3] = mmu_read_word_print(curaddr+videooffset*2+0);
    d[2] = mmu_read_word_print(curaddr+videooffset*2+2);
    d[1] = mmu_read_word_print(curaddr+videooffset*2+4);
    d[0] = mmu_read_word_print(curaddr+videooffset*2+6);
    lastpos = curaddr+videooffset*2;
  }
  
  c = ((((d[0]>>(15-pxlnum))&1)<<3)|
       (((d[1]>>(15-pxlnum))&1)<<2)|
       (((d[2]>>(15-pxlnum))&1)<<1)|
       (((d[3]>>(15-pxlnum))&1)));
  return c;
{'fraction_non_alphanumeric': 0.13806451612903226, 'fraction_numerical': 0.04564516129032258, 'mean_word_length': 4.107907742998353, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4835773', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2427, 'n_words': 651}