summaryrefslogtreecommitdiffstats
path: root/abc80_mem.c
blob: 6fa46ab4a2e7aac9530b33067c57ddbb7ba5f428 (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
#include <stdio.h>
#include <malloc.h>

#include "screen.h"
#include "z80.h"

#define MEMORY_SIZE	Z80_ADDRESS_LIMIT

#define ROM_START	(0x0000)
#define ROM_END  	(0x4000)
#define VIDEO_START	(0x7400) /* GeJo2 80 tecken */
#define VIDEO_MASK	(0xf400)
#define VIDEO_LEN       (0x0400)
#define RAM_START       (0x8000)

static uint8_t memory[MEMORY_SIZE];

/*
 * Macros to determine quickly if an address is writeable.
 */
#define WRITEABLE(address)  ((address) >= ROM_END)

#define DEBUG_READ(address)	0

void mem_init(void)
{
}


/*
 * hack to let us initialize the ROM memory
 */
uint8_t *mem_rom_address(void)
{
    return memory;
}


/*
 * hack to get a pointer into the Z80 "memory"
 */
uint8_t *mem_get_addr(uint16_t address)
{
    return &memory[address];
}


uint8_t mem_read(uint16_t address)
{
    if (DEBUG_READ(address)) {
	printf("Accessed:\t0x%04x %5d : %02x\n",
	       address, address, memory[address]);
    }
    return memory[address];
}


void mem_write(uint16_t address, uint8_t value)
{
  if ((address & VIDEO_MASK) == VIDEO_START) {
	/*
	 * Speed hack -- check to see if the character has actually changed.
	 * Only call the video emulator if it has.
	 */
	if (memory[address] != value)
	{
	    memory[address] = value;
	    screen_write(address, value);
	}

    } else if (WRITEABLE(address)) {
	/* write to RAM */
	memory[address] = value;
    }
}


/*
 * Words are stored with the low-order byte in the lower address.
 */
uint16_t mem_read_word(uint16_t address)
{
    uint8_t b0, b1;

    b0 = memory[address];
    b1 = memory[(uint16_t)(address + 1)];

    if (DEBUG_READ(address)) {
	printf("Accessed:\t0x%04x %5d : %02x%02x\n",
	       address, address, b1, b0);
    }

    return (b1 << 8) + b0;
}


void mem_write_word(uint16_t address, uint16_t value)
{
    mem_write(address, value);
    mem_write(address+1, value >> 8);
}


/*
 * Block move instructions, for LDIR and LDDR instructions.
 *
 * Direction is either +1 or -1.
 *
 * Note that a count of zero => move 64K bytes.
 */
void 
mem_block_transfer(uint16_t dest, uint16_t source, int direction, uint16_t count)
{
    if(direction > 0)
    {
        do
        {
            mem_write(dest++, mem_read(source++));
            count--;
        }
        while(count);
    }
    else
    {
        do
        {
            mem_write(dest--, mem_read(source--));
            count--;
        }
        while(count);
    }
}