muddle/src/memory.c

102 lines
3 KiB
C

/*
* Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include "memory.h"
#include "story.h"
#include "state.h"
#include "zork1.h"
// The F256 has 512k of RAM. We use everything except the lower 64k.
static uint8_t _RAM[1024 * (512 - 64)];
uint8_t memoryByte(uint16_t address) {
return _RAM[address];
}
void memorySetByte(uint16_t address, uint8_t value) {
_RAM[address] = value;
}
void memorySetWord(uint16_t address, uint16_t value) {
_RAM[address] = (value & 0xff00) << 8; // MSB first.
_RAM[address + 1] = value & 0x00ff;
}
uint32_t memoryUnpackAddress(uint16_t address, uint8_t type) {
switch (storyVersion()) {
case 1:
case 2:
case 3:
return (uint32_t)address * 2;
case 4:
case 5:
return (uint32_t)address * 4;
case 6:
if (type == MEMORY_ROUTINE) {
return ((uint32_t)address * 4) + storyRoutinesOffset();
} else {
return ((uint32_t)address * 4) + storyStringsOffset();
}
case 8:
return (uint32_t)address * 8;
}
return ((uint16_t)_RAM[address] << 8) | ((uint16_t)_RAM[address + 1]);
}
uint16_t memoryWord(uint16_t address) {
return ((uint16_t)_RAM[address] << 8) | ((uint16_t)_RAM[address + 1]);
}
void memoryLoadStory(void) {
// For now, we just copy an embedded Zork 1 into RAM.
memcpy(_RAM, zork1, zork1_len);
// Later, we probably want to see if the user has a memory expansion
// installed. If they do, we can use it and then use the lower memory
// for graphics and sound.
// Zero out all state data.
memset(&__state, 0, sizeof(stateT));
// Currently no status bar.
POKE(STORY_FLAG_V3, PEEK(STORY_FLAG_V3) | STORY_FLAG_V3_STATUS_LINE_NOT_AVAILABLE);
// Uncomment for status bar and window splitting.
//POKE(STORY_FLAG_V3, PEEK(STORY_FLAG_V3) & ~STORY_FLAG_V3_STATUS_LINE_NOT_AVAILABLE);
//POKE(STORY_FLAG_V3, PEEK(STORY_FLAG_V3) | STORY_FLAG_V3_SCREEN_SPLITTING);
// V6+ this is the address of main(), not just a raw starting address.
__state.pc = storyInitialPC();
__state.sp = 0;
}