1 typedef unsigned char UINT8;
2 typedef unsigned int UINT32;
3 typedef void genf(void);
5 struct _subtable_data {
8 typedef struct _subtable_data subtable_data;
11 subtable_data subtable[64];
13 typedef struct _table_data table_data;
14 struct _addrspace_data {
18 typedef struct _addrspace_data addrspace_data;
20 addrspace_data space[3];
22 typedef struct _cpu_data cpu_data;
23 static cpu_data cpudata[8];
24 static void install_mem_handler(addrspace_data * space, int iswrite,
25 int databits, int ismatchmask, offs_t start,
26 offs_t end, offs_t mask, offs_t mirror,
27 genf * handler, int isfixed,
28 const char *handler_name);
29 static void reallocate_subtable(table_data * tabledata, UINT8 subentry);
30 static void release_subtable(table_data * tabledata, UINT8 subentry);
31 void *_memory_install_read_handler(int cpunum, int spacenum, offs_t start,
32 offs_t end, offs_t mask, offs_t mirror,
33 int handler, const char *handler_name)
35 addrspace_data *space = &cpudata[cpunum].space[spacenum];
36 install_mem_handler(space, 0, 8, 0, start, end, mask, mirror,
37 (genf *) handler, 0, handler_name);
39 static void install_mem_handler(addrspace_data * space, int iswrite,
40 int databits, int ismatchmask, offs_t start,
41 offs_t end, offs_t mask, offs_t mirror,
42 genf * handler, int isfixed,
43 const char *handler_name)
45 offs_t lmirrorbit[18], lmirrorbits, hmirrorbit[(32 - 18)], hmirrorbits,
46 lmirrorcount, hmirrorcount;
47 table_data *tabledata = iswrite ? &space->write : &space->read;
48 UINT8 idx, prev_entry = 0;
49 int cur_index, prev_index = 0;
51 for (i = (32 - 18); hmirrorcount < (1 << hmirrorbits); hmirrorcount++) {
52 offs_t hmirrorbase = 0;
53 if (cur_index == ((end + hmirrorbase) >> (32 - 18))) {
55 && prev_entry == tabledata->table[cur_index]) {
56 if (tabledata->table[cur_index] >= (256 - 64))
57 release_subtable(tabledata,
60 if (tabledata->table[prev_index] >= (256 - 64))
61 reallocate_subtable(tabledata,
65 prev_entry = tabledata->table[cur_index];
67 for (lmirrorcount = 0;
68 lmirrorcount < (1 << lmirrorbits); lmirrorcount++) {
69 offs_t lmirrorbase = hmirrorbase;
70 for (i = 0; i < lmirrorbits; i++)
71 if (lmirrorcount & (1 << i))
72 lmirrorbase |= lmirrorbit[i];
76 static void reallocate_subtable(table_data * tabledata, UINT8 subentry)
78 UINT8 subindex = subentry - (256 - 64);
79 if (tabledata->subtable[subindex].usecount <= 0)
81 ("Called reallocate_subtable on a table with a usecount of 0\n");
83 static void release_subtable(table_data * tabledata, UINT8 subentry)
85 UINT8 subindex = subentry - (256 - 64);
86 if (tabledata->subtable[subindex].usecount <= 0)
88 ("Called release_subtable on a table with a usecount of 0\n");