summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/cpu/ssem/ssem.cpp
blob: c15b984c5469317cd1def1249425c4f5da8e58c2 (plain) (blame)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz
/*
    Manchester Small-Scale Experimental Machine (SSEM) emulator

    Written by Ryan Holtz
*/

#include "emu.h"
#include "debugger.h"
#include "ssem.h"

CPU_DISASSEMBLE( ssem );


#define SSEM_DISASM_ON_UNIMPL           0
#define SSEM_DUMP_MEM_ON_UNIMPL         0

#define INSTR       ((op >> 13) & 7)
#define ADDR        (op & 0x1f)

/*****************************************************************************/

// The SSEM stores its data, visually, with the leftmost bit corresponding to the least significant bit.
// The de facto snapshot format for other SSEM simulators stores the data physically in that format as well.
// Therefore, in MESS, every 32-bit word has its bits reversed, too, and as a result the values must be
// un-reversed before being used.
static inline uint32_t reverse(uint32_t v)
{
	// Taken from http://www-graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
	// swap odd and even bits
	v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);
	// swap consecutive pairs
	v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);
	// swap nibbles ...
	v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);
	// swap bytes
	v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);
	// swap 2-byte long pairs
	v = ( v >> 16             ) | ( v               << 16);

	return v;
}

inline uint32_t ssem_device::program_read32(uint32_t address)
{
	uint32_t v = 0;
	// The MAME core does not have a good way of specifying a minimum datum size that is more than
	// 8 bits in width.  The minimum datum width on the SSEM is 32 bits, so we need to quadruple
	// the address value to get the appropriate byte index.
	address <<= 2;

	v |= m_program->read_byte(address + 0) << 24;
	v |= m_program->read_byte(address + 1) << 16;
	v |= m_program->read_byte(address + 2) <<  8;
	v |= m_program->read_byte(address + 3) <<  0;

	return reverse(v);
}

inline void ssem_device::program_write32(uint32_t address, uint32_t data)
{
	uint32_t v = reverse(data);

	// The MAME core does not have a good way of specifying a minimum datum size that is more than
	// 8 bits in width.  The minimum datum width on the SSEM is 32 bits, so we need to quadruple
	// the address value to get the appropriate byte index.
	address <<= 2;

	m_program->write_byte(address + 0, (v >> 24) & 0x000000ff);
	m_program->write_byte(address + 1, (v >> 16) & 0x000000ff);
	m_program->write_byte(address + 2, (v >>  8) & 0x000000ff);
	m_program->write_byte(address + 3, (v >>  0) & 0x000000ff);
	return;
}

/*****************************************************************************/

DEFINE_DEVICE_TYPE(SSEMCPU, ssem_device, "ssem_cpu", "SSEM CPU")

//-------------------------------------------------
//  ssem_device - constructor
//-------------------------------------------------

ssem_device::ssem_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: cpu_device(mconfig, SSEMCPU, tag, owner, clock)
	, m_program_config("program", ENDIANNESS_LITTLE, 8, 16)
	, m_pc(1)
	, m_shifted_pc(1<<2)
	, m_a(0)
	, m_halt(0)
	, m_icount(0)
{
	// Allocate & setup
}


void ssem_device::device_start()
{
	m_program = &space(AS_PROGRAM);

	// register our state for the debugger
	state_add(STATE_GENPC,     "GENPC",     m_pc).noshow();
	state_add(STATE_GENPCBASE, "CURPC",     m_pc).noshow();
	state_add(STATE_GENFLAGS,  "GENFLAGS",  m_halt).callimport().callexport().formatstr("%1s").noshow();
	state_add(SSEM_PC,         "PC",        m_shifted_pc).mask(0xffff);
	state_add(SSEM_A,          "A",         m_a).mask(0xffffffff);
	state_add(SSEM_HALT,       "HALT",     m_halt).mask(0xf);

	/* setup regtable */
	save_item(NAME(m_pc));
	save_item(NAME(m_a));
	save_item(NAME(m_halt));

	// set our instruction counter
	m_icountptr = &m_icount;
}

void ssem_device::device_stop()
{
}

void ssem_device::device_reset()
{
	m_pc = 1;
	m_shifted_pc = m_pc << 2;
	m_a = 0;
	m_halt = 0;
}


//-------------------------------------------------
//  memory_space_config - return the configuration
//  of the specified address space, or nullptr if
//  the space doesn't exist
//-------------------------------------------------

const address_space_config *ssem_device::memory_space_config(address_spacenum spacenum) const
{
	if (spacenum == AS_PROGRAM)
	{
		return &m_program_config;
	}
	return nullptr;
}


//-------------------------------------------------
//  state_string_export - export state as a string
//  for the debugger
//-------------------------------------------------

void ssem_device::state_string_export(const device_state_entry &entry, std::string &str) const
{
	switch (entry.index())
	{
		case STATE_GENFLAGS:
			str = string_format("%c", m_halt ? 'H' : '.');
			break;
	}
}


//-------------------------------------------------
//  disasm_min_opcode_bytes - return the length
//  of the shortest instruction, in bytes
//-------------------------------------------------

uint32_t ssem_device::disasm_min_opcode_bytes() const
{
	return 4;
}


//-------------------------------------------------
//  disasm_max_opcode_bytes - return the length
//  of the longest instruction, in bytes
//-------------------------------------------------

uint32_t ssem_device::disasm_max_opcode_bytes() const
{
	return 4;
}


//-------------------------------------------------
//  disasm_disassemble - call the disassembly
//  helper function
//-------------------------------------------------

offs_t ssem_device::disasm_disassemble(std::ostream &stream, offs_t pc, const uint8_t *oprom, const uint8_t *opram, uint32_t options)
{
	extern CPU_DISASSEMBLE( ssem );
	return CPU_DISASSEMBLE_NAME(ssem)(this, stream, pc, oprom, opram, options);
}


//**************************************************************************
//  CORE EXECUTION LOOP
//**************************************************************************

//-------------------------------------------------
//  execute_min_cycles - return minimum number of
//  cycles it takes for one instruction to execute
//-------------------------------------------------

uint32_t ssem_device::execute_min_cycles() const
{
	return 1;
}


//-------------------------------------------------
//  execute_max_cycles - return maximum number of
//  cycles it takes for one instruction to execute
//-------------------------------------------------

uint32_t ssem_device::execute_max_cycles() const
{
	return 1;
}


//-------------------------------------------------
//  execute_input_lines - return the number of
//  input/interrupt lines
//-------------------------------------------------

uint32_t ssem_device::execute_input_lines() const
{
	return 0;
}


//-------------------------------------------------
//  execute_set_input - set the state of an input
//  line during execution
//-------------------------------------------------

void ssem_device::execute_set_input(int inputnum, int state)
{
}


//-------------------------------------------------
//  execute_run - execute a timeslice's worth of
//  opcodes
//-------------------------------------------------

void ssem_device::execute_run()
{
	uint32_t op;

	m_pc &= 0x1f;
	m_shifted_pc = m_pc << 2;

	while (m_icount > 0)
	{
		debugger_instruction_hook(this, m_pc);

		op = program_read32(m_pc);

		if( !m_halt )
		{
			m_pc++;
			m_shifted_pc = m_pc << 2;
		}
		else
		{
			op = 0x0000e000;
		}

		switch (INSTR)
		{
			case 0:
				// JMP: Move the value at the specified address into the Program Counter.
				m_pc = program_read32(ADDR) + 1;
				m_shifted_pc = m_pc << 2;
				break;
			case 1:
				// JRP: Add the value at the specified address to the Program Counter.
				m_pc += (int32_t)program_read32(ADDR);
				m_shifted_pc = m_pc << 2;
				break;
			case 2:
				// LDN: Load the accumulator with the two's-complement negation of the value at the specified address.
				m_a = (uint32_t)(0 - (int32_t)program_read32(ADDR));
				break;
			case 3:
				// STO: Store the value in the accumulator at the specified address.
				program_write32(ADDR, m_a);
				break;
			case 4:
			case 5:
				// SUB: Subtract the value at the specified address from the accumulator.
				m_a -= program_read32(ADDR);
				break;
			case 6:
				// CMP: If the accumulator is less than zero, skip the next opcode.
				if((int32_t)(m_a) < 0)
				{
					m_pc++;
					m_shifted_pc = m_pc << 2;
				}
				break;
			case 7:
				// STP: Halt the computer.
				m_halt = 1;
				break;
			default:
				break;
		}

		--m_icount;
	}
}