summaryrefslogtreecommitdiff
path: root/reverse-engineering/dosbox_snif/main_validate_code.c
blob: afb32aec03836c0f7837e19560b45e04d57b645d (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "dos_lemm.h"
#include "dos_lemm_sim.h"
#include "rsp.h"
#include "rsp_lemm.h"
#include "utils.h"

#include <string.h> /* memset(), memcmp() */
#include <stdio.h> /* snprintf(), printf() */
#include <stdlib.h> /* free() */
#include <stddef.h> /* offsetof() */
//#include <unistd.h> /* sleep() */

int game_data_diff(struct game_data *g1, struct game_data *g2, char diff[], size_t difflen) {
	int i, start, end, lemm, lemmcount, off;
	uint8_t *g1_raw = (uint8_t *)g1;
	uint8_t *g2_raw = (uint8_t *)g2;


	start=offsetof(struct game_data, lemm_count_to_process);
	end=offsetof(struct game_data, lemmings);
	for (i=start; i<end; i++) {
		if ( g1_raw[i] != g2_raw[i] ) {
			snprintf(diff, difflen, "game_data[0x%02X] : %02hhX / %02hhX", i, g1_raw[i], g2_raw[i]);
			return 1;
		}
	}

	lemmcount = imin(g1->lemm_level_count, (sizeof(g1->lemmings) / sizeof(struct _lemm_data)));
//	printf("lemmcount:%i\n", lemmcount);

	for (lemm=0; lemm < lemmcount; lemm++ ) {
		start=offsetof(struct game_data, lemmings) + lemm * sizeof(struct _lemm_data);
//		printf("lemmings[%i] : [0x%0X]\n", lemm, start);

		for (off=0; off<sizeof(struct _lemm_data); off++) {
			i=start+off;
			if ( g1_raw[i] != g2_raw[i] ) {
				snprintf(diff, difflen, "lemmings[%i][0x%02X] : %02hhX / %02hhX", lemm, off, g1_raw[i], g2_raw[i]);
				return 1;
			}
			
		}

	}

	if ( memcmp(g1,g2,sizeof(struct game_data)) != 0 ) {
		snprintf(diff, difflen, "Unknown");
		return 1;
	}

	return 0;
}

void _mem_dump(struct rsp_state *rsp, struct game_data *g) {
	unsigned int bs=0x100; /* RSP memdump block size */

	unsigned int i, addr, size, offset, byte;
	char command[16], hex_byte[3];

	hex_byte[2]='\0';
	for (offset=0 ; offset < sizeof(struct game_data) ; offset += bs) {
		size = imin(bs,sizeof(struct game_data)-offset);
		addr = (0xb55 << 4) + offset;
		snprintf(command, 15, "m%06x,0x%x", addr, size);
		//printf("-> %s\n", command);
		rsp_query(rsp, command);
		if ( rsp_decode(rsp) != size*2) {
			printf("%06x : Bug\n", addr);
			break;
		}

		//printf("%06x : %s\n", addr, rsp->decoded);
		for (i=0;i<size;i++) {
			memcpy(hex_byte, rsp->decoded+(i*2), 2);
			if ( sscanf(hex_byte, "%x", &byte) != 1 ) {
				printf("Bug decode\n");
				break;
			}
			((char *)g)[offset+i] = byte;
			//printf("((char *)&g)[0x%02x] = 0x%02x\n", offset+i, byte);
		}
	}
	//printf("\n");
}

int main(int argc, char *argv[]) {
	int rv, end=0, loops;
	char ds_si[10];
	struct rsp_state rsp;
	char msg_diff[256];
	struct game_data g_before, g_after, g_simulated;


	rv=rsp_lemm_init(&rsp, ds_si);
	if ( rv != 0 ) {
		printf("Error rsp_lemm_init() returns %i\n", rv);
		return 1;
	}

	rsp_query(&rsp, "Z0,2F7B,1"); // Set execution breakpoint at 0208:0EFB (0x2F7B, return point of move_lemmings() )
	if ( rsp_check_and_clear(&rsp, "OK") != 0 ) {
		printf("Error when setting addition breakpoint\n");
		return 1;
	}

	loops=0;
	while (!end) {
		loops++;
		rsp_query(&rsp, "c"); // Continue
		if ( rsp.replied != 1 ) {
			printf("Bug 03\n");
			continue;
		}
		//rsp_recv_full(&rsp);
		if ( rsp_check_and_clear(&rsp, "S05") != 0 ) {
			printf("Bug 04\n");
			continue;
		}

		rsp_query(&rsp, "p8"); // Read $eip

		//printf("DEBUG : rsp->response_bom+1 : %s\n", rsp.response_bom + 1);
		if ( rsp_check_and_clear(&rsp, "c4380000") == 0 ) {
			// Beginning of move_lemmings()
			_mem_dump(&rsp,&g_before);

			// Exec simulation
			memcpy(&g_simulated,&g_before,sizeof(struct game_data));
			move_lemmings(&g_simulated);
		} else {
			// End of move_lemmings()
			_mem_dump(&rsp,&g_after);

			// Compare simulation results and orignal code results
			if ( game_data_diff(&g_after, &g_simulated, msg_diff, sizeof(msg_diff)) != 0 ) {
				printf("Diff found : %s\n", msg_diff);
				//(void) scanf("nothing");
				//sleep(1);
			}
		}

	}

	rsp_quit(&rsp);
	return 0;
}