File: md-joe.cpp

package info (click to toggle)
dgen 1.23-12
  • links: PTS
  • area: non-free
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 2,576 kB
  • ctags: 3,556
  • sloc: ansic: 45,403; cpp: 4,414; sh: 1,960; makefile: 86
file content (76 lines) | stat: -rw-r--r-- 1,865 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// DGen/SDL v1.15+

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "md.h"
#include "decode.h"

// These are my (Joe's) functions added to the md class.

// This takes a comma or whitespace-separated list of Game Genie and/or hex 
// codes to patch the ROM with.
void md::patch(const char *list)
{
  static const char delims[] = " \t\n,";
  char *worklist, *tok;
  struct patch p;
  
  // Copy the given list to a working list so we can strtok it
  worklist = (char*)malloc(strlen(list)+1);
  strcpy(worklist, list);

  for(tok = strtok(worklist, delims); tok; tok = strtok(NULL, delims))
    {
      // If it's empty, toss it
      if(*tok == '\0') continue;
      // Decode it
      decode(tok, &p);
      // Discard it if it was bad code
      if((signed)p.addr == -1) {
	printf("Bad patch \"%s\"\n", tok);
	continue;
      }
      // Put it into the ROM (remember byteswapping)
      printf("Patch \"%s\" -> %06X:%04X\n", tok, p.addr, p.data);
      rom[p.addr] = (char)(p.data & 0xFF);
      rom[p.addr+1] = (char)((p.data & 0xFF00) >> 8);
    }
  // Done!
  free(worklist);
  return;
}

// Get/put saveram from/to FILE*'s
void md::get_save_ram(FILE *from)
{
  // Pretty simple, just read the saveram raw
  fread((void*)saveram, 1, save_len, from);
}

void md::put_save_ram(FILE *into)
{
  // Just the opposite of the above :)
  fwrite((void*)saveram, 1, save_len, into);
}

// Dave: This is my code, but I thought it belonged here
// Joe: Thanks Dave! No problem ;)
static unsigned short calculate_checksum(unsigned char *rom,int len)
{
  unsigned short checksum=0;
  int i;
  for (i=512;i<=(len-2);i+=2)
  {
    checksum+=(rom[i+1]<<8);
    checksum+=rom[i+0];
  }
  return checksum;
}

void md::fix_rom_checksum()
{
  unsigned short cs; cs=calculate_checksum(rom,romlen);
  if (romlen>=0x190) { rom[0x18f]=cs>>8; rom[0x18e]=cs&255; }
}