OXYGENHUB
◆ rrdc-perf enhanced-x16-emulator / tests / support / signature.c
1.3 KiB · C · last changed chore(): initial commit 26 days ago source blame history raw
9120e874 Andy McCall 1
#include "signature.h"
2
#include "fake_devices.h"
3
4
#include "memory.h"
5
#include "cpu/registers.h"
6
7
#include <stdint.h>
8
9
extern struct regs regs;
10
extern uint32_t clockticks6502;
11
extern uint8_t *RAM;
12
13
// FNV-1a. Chosen because it is short enough to read and verify at a glance,
14
// which matters more here than distribution: this is change detection, not
15
// cryptography.
16
static void
17
mix(uint64_t *h, const void *data, size_t len)
18
{
19
	const uint8_t *p = (const uint8_t *)data;
20
	for (size_t i = 0; i < len; i++) {
21
		*h ^= p[i];
22
		*h *= 0x100000001b3ULL;
23
	}
24
}
25
26
machine_sig_t
27
machine_signature(void)
28
{
29
	uint64_t h = 0xcbf29ce484222325ULL;
30
31
	// CPU: registers and what they have cost so far.
32
	mix(&h, &regs, sizeof regs);
33
	mix(&h, &clockticks6502, sizeof clockticks6502);
34
35
	// Memory: the low 64K, which is everything these fixtures touch.
36
	if (RAM != NULL) {
37
		mix(&h, RAM, 0x10000);
38
	}
39
40
	// Which banks are mapped, since a read through a window depends on it.
41
	uint8_t ram_bank = memory_get_ram_bank();
42
	uint8_t rom_bank = memory_get_rom_bank();
43
	mix(&h, &ram_bank, sizeof ram_bank);
44
	mix(&h, &rom_bank, sizeof rom_bank);
45
46
	// Devices: how often each was touched, and whether it moved.
47
	mix(&h, fake_dev, sizeof(fake_dev_log_t) * FAKE_DEV_COUNT);
48
49
	machine_sig_t sig = { h };
50
	return sig;
51
}
52
53
int
54
machine_sig_equal(machine_sig_t a, machine_sig_t b)
55
{
56
	return a.value == b.value;
57
}