pokemon/main.cpp
2023-08-04 18:45:50 +00:00

745 lines
23 KiB
C++

#include <cstdint>
#include "vdp2.h"
#include "vdp1.h"
#include "scu.h"
#include "smpc.h"
#include "sh2.h"
#include "common/copy.hpp"
#include "common/vdp2_func.hpp"
#include "common/intback.hpp"
#include "input.hpp"
#include "vram.hpp"
#include "font.hpp"
#include "gen/maps.hpp"
#include "gen/sprites.hpp"
#include "map_objects.hpp"
#include "coordinates.hpp"
#include "render_map.hpp"
#include "actor.hpp"
#include "ledge_tiles.hpp"
#include "graphic.hpp"
#include "menu.hpp"
#include "pokemon.hpp"
#include "pokemon_instance.hpp"
#include "menu/stats.hpp"
static int32_t pokemon_raw_index = 0;
struct draw_t {
struct {
uint16_t font; // div 32
uint16_t tilesets[tileset_t::count]; // div 32
uint16_t spritesheets[spritesheet_t::count]; // div 128
struct {
uint16_t front;
uint16_t back;
} pokemon[pokemon_t::count]; // div 16
} base_pattern;
};
struct state_t {
enum map_t::map map;
enum map_t::map last_map;
draw_t draw;
actor_t player;
};
static state_t state = { map_t::pallet_town, map_t::last_map, 0 };
uint32_t load_tileset(uint32_t top, enum tileset_t::tileset tileset)
{
const uint32_t base_address = top = cell_data(tilesets[tileset].tileset, top);
state.draw.base_pattern.tilesets[tileset] = base_address / 32;
return top;
}
uint32_t load_spritesheet(uint32_t top, enum spritesheet_t::spritesheet spritesheet)
{
const spritesheet_t& s = spritesheets[spritesheet];
const uint32_t base_address = top = character_pattern_table(s.spritesheet, top);
state.draw.base_pattern.spritesheets[spritesheet] = base_address / 128;
return top;
}
uint32_t load_pokemon(uint32_t top, enum pokemon_t::pokemon _pokemon)
{
const pokemon_t& p = pokemon[_pokemon];
const uint32_t base_address_front = top = character_pattern_table(p.pic.front, top);
const uint32_t base_address_back = top = character_pattern_table(p.pic.back, top);
state.draw.base_pattern.pokemon[_pokemon].front = base_address_front / 16;
state.draw.base_pattern.pokemon[_pokemon].back = base_address_back / 16;
return top;
}
void load_vram()
{
vdp2.reg.CYCA0 = 0xeeee'eeee;
vdp2.reg.CYCA1 = 0xeeee'eeee;
vdp2.reg.CYCB0 = 0xeeee'eeee;
vdp2.reg.CYCB1 = 0xeeee'eeee;
uint32_t vdp2_top = (sizeof (union vdp2_vram));
vdp2_top = load_font(vdp2_top);
state.draw.base_pattern.font = vdp2_top / 32;
for (uint32_t i = 0; i < tileset_t::count; i++)
vdp2_top = load_tileset(vdp2_top, static_cast<enum tileset_t::tileset>(i));
vdp2.reg.CYCA0 = 0x01ff'ffff;
vdp2.reg.CYCA1 = 0x01ff'ffff;
vdp2.reg.CYCB0 = 0x45ff'ffff;
vdp2.reg.CYCB1 = 0x45ff'ffff;
uint32_t vdp1_top = (sizeof (union vdp1_vram));
for (uint32_t i = 0; i < spritesheet_t::count; i++)
vdp1_top = load_spritesheet(vdp1_top, static_cast<enum spritesheet_t::spritesheet>(i));
for (uint32_t i = 0; i < pokemon_t::count; i++)
vdp1_top = load_pokemon(vdp1_top, static_cast<enum pokemon_t::pokemon>(i));
}
static inline uint32_t facing_offset(const actor_t::direction facing)
{
switch (facing) {
default: [[fallthrough]];
case actor_t::down: return 0;
case actor_t::up: return 1;
case actor_t::left: return 2;
case actor_t::right: return 2;
}
}
static inline uint32_t facing_inverted(const actor_t::direction facing, const uint32_t animation_cycle)
{
switch (facing) {
default: [[fallthrough]];
case actor_t::down: [[fallthrough]];
case actor_t::up:
return (animation_cycle & 1) ? CTRL__DIR__INVERTED_HORIZONTALLY : CTRL__DIR__NOT_INVERTED;
case actor_t::left:
return CTRL__DIR__NOT_INVERTED;
case actor_t::right:
return CTRL__DIR__INVERTED_HORIZONTALLY;
}
}
void render_sprite(const uint32_t ix, const enum spritesheet_t::spritesheet sprite_id,
const enum actor_t::direction facing, const uint32_t animation_frame, const uint32_t animation_cycle,
const screen_t& screen, const offset_t& offset,
int32_t y_offset)
{
const uint32_t sprite_offset = facing_offset(facing) + animation_frame;
const uint32_t base_pattern = state.draw.base_pattern.spritesheets[sprite_id];
const uint32_t character_address = ((base_pattern + sprite_offset) * 128) / 8;
vdp1.vram.cmd[ix].CTRL = CTRL__JP__JUMP_NEXT | CTRL__COMM__NORMAL_SPRITE | facing_inverted(facing, animation_cycle);
vdp1.vram.cmd[ix].LINK = 0;
// The "end code" is 0xf, which is being used in the mai sprite palette. If
// both transparency and end codes are enabled, it seems there are only 14
// usable colors in the 4-bit color mode.
vdp1.vram.cmd[ix].PMOD = PMOD__ECD | PMOD__COLOR_MODE__COLOR_BANK_16;
vdp1.vram.cmd[ix].COLR = COLR__COLOR_BANK__4BPP__PALETTE(1)
| COLR__COLOR_BANK__TYPE0__PR(1);
vdp1.vram.cmd[ix].SRCA = character_address;
vdp1.vram.cmd[ix].SIZE = SIZE__X(16) | SIZE__Y(16);
vdp1.vram.cmd[ix].XA = (cell_offset::x * 8) + screen.x * 16 - offset.x;
vdp1.vram.cmd[ix].YA = (cell_offset::y * 8) + screen.y * 16 + y_offset - offset.y;
}
uint32_t pokemon_sprite_dimension(const uint32_t size)
{
switch (size) {
default: [[fallthrough]];
case 256: return 32;
case 400: return 40;
case 576: return 48;
case 784: return 56;
}
}
void render_pokemon(const uint32_t ix, const enum pokemon_t::pokemon pokemon_id,
const screen_cell_t& screen_cell, bool horizontal_inversion)
{
const uint32_t base_pattern = state.draw.base_pattern.pokemon[pokemon_id].front;
const uint32_t character_address = (base_pattern * 16) / 8;
const uint32_t dimension = pokemon_sprite_dimension(pokemon[pokemon_id].pic.front.size);
int32_t x_offset;
int32_t y_offset;
switch (dimension) {
default:
case 40: x_offset = 2; y_offset = 2; break;
case 48: x_offset = 1; y_offset = 1; break;
case 56: x_offset = 1; y_offset = 0; break;
}
uint16_t dir = horizontal_inversion ? CTRL__DIR__INVERTED_HORIZONTALLY : 0;
vdp1.vram.cmd[ix].CTRL = CTRL__JP__JUMP_NEXT | CTRL__COMM__NORMAL_SPRITE | dir;
vdp1.vram.cmd[ix].LINK = 0;
// The "end code" is 0xf, which is being used in the mai sprite palette. If
// both transparency and end codes are enabled, it seems there are only 14
// usable colors in the 4-bit color mode.
vdp1.vram.cmd[ix].PMOD = PMOD__ECD | PMOD__COLOR_MODE__COLOR_BANK_16;
vdp1.vram.cmd[ix].COLR = COLR__COLOR_BANK__4BPP__PALETTE(0)
| COLR__COLOR_BANK__TYPE0__PR(0);
vdp1.vram.cmd[ix].SRCA = character_address;
vdp1.vram.cmd[ix].SIZE = SIZE__X(dimension) | SIZE__Y(dimension);
vdp1.vram.cmd[ix].XA = (cell_offset::x * 8) + (screen_cell.x + x_offset) * 8;
vdp1.vram.cmd[ix].YA = (cell_offset::y * 8) + (screen_cell.y + y_offset) * 8;
}
void render_sprites(const offset_t& offset)
{
uint32_t ix = 2;
const uint32_t animation_frame = ((state.player.frame & 0b1000) != 0) * 3;
render_sprite(ix,
spritesheet_t::red,
state.player.facing, animation_frame, state.player.cycle,
{4, 4},
{0, 0},
state.player.y_offset());
ix++;
render_pokemon(ix, static_cast<enum pokemon_t::pokemon>(pokemon_raw_index),
{0, 0},
true); // invert horizontally
ix++;
const object_t& obj = map_objects[state.map];
for (uint32_t i = 0; i < obj.object_length; i++) {
const object_event_t& event = obj.object_events[i];
const world_t world = { event.position.x, event.position.y };
render_sprite(ix,
event.sprite_id,
actor_t::down, 0, 0,
world.to_screen(state.player.world),
offset,
-4);
ix++;
}
constexpr uint16_t top_x = 80 - 1;
constexpr uint16_t top_y = 48 - 1;
constexpr uint16_t bot_x = 239 + 1;
constexpr uint16_t bot_y = 191 + 1;
vdp1.vram.cmd[ix].CTRL = CTRL__JP__JUMP_NEXT | CTRL__COMM__POLYLINE;
vdp1.vram.cmd[ix].LINK = 0;
// "Set [ECD] to '1' for polygons, polylines, and lines"
// "Be sure to set [SPD] to '1' for polygons, polylines, and lines"
//
// The "user clip mode" bit is not set in PMOD here, so setting "user clip
// coordinates" has no effect on this draw command. However, "system clip
// coordinates" and "local coordinates" are always applied, and must be set to
// reasonable values.
vdp1.vram.cmd[ix].PMOD = PMOD__ECD | PMOD__SPD;
vdp1.vram.cmd[ix].COLR = COLR__RGB | rgb15(255, 0, 255);
vdp1.vram.cmd[ix].XA = top_x;
vdp1.vram.cmd[ix].YA = top_y;
vdp1.vram.cmd[ix].XB = bot_x;
vdp1.vram.cmd[ix].YB = top_y;
vdp1.vram.cmd[ix].XC = bot_x;
vdp1.vram.cmd[ix].YC = bot_y;
vdp1.vram.cmd[ix].XD = top_x;
vdp1.vram.cmd[ix].YD = bot_y;
ix++;
vdp1.vram.cmd[ix].CTRL = CTRL__END;
}
void render_map()
{
const map_t& map = maps[state.map];
const uint32_t base_pattern = state.draw.base_pattern.tilesets[map.tileset];
vdp2.reg.PNCN0 = PNCN0__N0PNB__1WORD | PNCN0__N0CNSM | PNCN0__N0SCN((base_pattern >> 10) & 0x1f);
for (int32_t y = (0 - 1); y < (9 + 2); y++) {
for (int32_t x = (0 - 2); x < (10 + 2); x++) {
render_screen(base_pattern,
map,
state.player.world,
{x, y}
);
}
}
vdp2.reg.BGON = BGON__N0ON | BGON__N0TPON | BGON__N1ON;
}
constexpr inline world_t direction_offset(const world_t& world, const enum actor_t::direction dir)
{
switch (dir) {
default: [[fallthrough]];
case actor_t::up: return {world.x , world.y - 1};
case actor_t::down: return {world.x , world.y + 1};
case actor_t::left: return {world.x - 1, world.y };
case actor_t::right: return {world.x + 1, world.y };
}
}
constexpr inline uint8_t get_tile_ix(const map_t& map, const world_t& coord)
{
// keep this synchronized with render_screen()
const uint8_t block_ix = get_block(map, coord.to_block());
const tileset_t& tileset = tilesets[map.tileset];
const uint8_t * block_start = &(tileset.blockset.start)[block_ix * 4 * 4];
const int32_t quadrant_x = 2 * (coord.x & 1);
const int32_t quadrant_y = 2 * (coord.y & 1);
const int32_t block_row = 4 * (quadrant_y + 1);
const uint8_t tile_ix = block_start[block_row + quadrant_x];
return tile_ix;
}
constexpr inline enum actor_t::collision collision(const map_t& map,
const world_t& world,
enum actor_t::direction direction)
{
const world_t coord = direction_offset(world, direction);
uint8_t collision_tile_ix = get_tile_ix(map, coord);
const tileset_t& tileset = tilesets[map.tileset];
for (uint32_t i = 0; i < tileset.collision.size; i++) {
if (tileset.collision.start[i] == collision_tile_ix)
return actor_t::passable;
}
// check ledge_tile pairs
uint8_t actor_tile_ix = get_tile_ix(map, world);
for (uint32_t i = 0; i < ledge_tiles_length; i++) {
const ledge_tile_t& lt = ledge_tiles[i];
if (direction == lt.direction
&& actor_tile_ix == lt.actor
&& collision_tile_ix == lt.collision) {
return actor_t::jump;
}
}
return actor_t::impassable;
}
constexpr inline void collision_move(const map_t& map, actor_t::direction dir)
{
const enum actor_t::collision c = collision(map, state.player.world, dir);
state.player.move(c, dir);
}
void change_maps()
{
const map_t& map = maps[state.map];
#define _has(_dir_) (map.connections[map_t::connection_t::_dir_].map != map_t::unconnected)
#define _get(_dir_) (maps[map.connections[map_t::connection_t::_dir_].map])
#define _offset(_dir_) (map.connections[map_t::connection_t::_dir_].offset)
const block_t block = state.player.world.to_block();
if (block.y < 0) {
// north
if (_has(north)) {
const map_t& north_map = _get(north);
state.player.world.y = ((north_map.height + block.y) << 1) | (state.player.world.y & 1);
state.player.world.x = ((block.x - _offset(north)) << 1) | (state.player.world.x & 1);
state.map = map.connections[map_t::connection_t::north].map;
}
} else if (block.y >= static_cast<int32_t>(map.height)) {
// south
if (_has(south)) {
state.player.world.y = ((block.y - map.height) << 1) | (state.player.world.y & 1);
state.player.world.x = ((block.x - _offset(south)) << 1) | (state.player.world.x & 1);
state.map = map.connections[map_t::connection_t::south].map;
}
} else if (block.x < 0) {
// west
if (_has(west)) {
const map_t& west_map = _get(west);
state.player.world.x = ((west_map.width + block.x) << 1) | (state.player.world.x & 1);
state.player.world.y = ((block.y - _offset(west)) << 1) | (state.player.world.y & 1);
state.map = map.connections[map_t::connection_t::west].map;
}
} else if (block.x >= static_cast<int32_t>(map.width)) {
// east
if (_has(east)) {
state.player.world.x = ((block.x - map.width) << 1) | (state.player.world.x & 1);
state.player.world.y = ((block.y - _offset(east)) << 1) | (state.player.world.y & 1);
state.map = map.connections[map_t::connection_t::east].map;
}
}
#undef _offset
#undef _get
#undef _has
}
constexpr inline bool is_outside(const enum map_t::map& map_id)
{
const map_t& map = maps[map_id];
switch (map.tileset) {
case tileset_t::overworld: [[fallthrough]];
case tileset_t::plateau:
return true;
default:
return false;
}
}
struct collision_direction_t {
enum actor_t::collision collision;
enum actor_t::direction direction;
};
constexpr inline collision_direction_t find_direction(const map_t& map, const world_t& world, const enum actor_t::direction facing)
{
// first, try `facing`, as this is typically correct in non- edge-cases
const enum actor_t::collision c_facing = collision(map, world, facing);
if (c_facing != actor_t::impassable)
return {c_facing, facing};
// otherwise try all other directions
// (this checks facing a second time for no reason)
constexpr enum actor_t::direction dirs[] = {actor_t::up, actor_t::down, actor_t::left, actor_t::right};
for (const enum actor_t::direction& dir : dirs) {
const enum actor_t::collision c = collision(map, world, dir);
if (c != actor_t::impassable)
return {c, dir};
}
return {actor_t::impassable, facing};
}
void update_warp()
{
if (state.player.moving) return;
world_t& coord = state.player.world;
for (uint32_t j = 0; j < map_objects[state.map].warp_length; j++) {
const warp_event_t& warp = map_objects[state.map].warp_events[j];
if (coord.x == warp.position.x && coord.y == warp.position.y) {
if (warp.destination.map == map_t::last_map) {
if (state.last_map == map_t::last_map) {
// use last_map as a sentinel value for "the player hasn't
// warped yet". This should never happen in normal gameplay.
continue;
}
state.map = state.last_map;
} else {
// Only change last_map if the current map is an "outside"
// map. Warps that use last_map are designed to be used this
// way.
if (is_outside(state.map)) state.last_map = state.map;
state.map = warp.destination.map;
}
// warp_index starts at 1
const warp_event_t& dest = map_objects[state.map].warp_events[warp.destination.warp_index - 1];
coord.x = dest.position.x;
coord.y = dest.position.y;
// force the player to move off of the warp
const collision_direction_t c_d = find_direction(maps[state.map], state.player.world, state.player.facing);
state.player.move(c_d.collision, c_d.direction);
// must return: because map state.map changed, the rest of this
// loop is invalid
return;
}
}
}
void check_sign()
{
const world_t coord = direction_offset(state.player.world, state.player.facing);
const map_t& map = maps[state.map];
const object_t& obj = map_objects[state.map];
for (uint32_t i = 0; i < obj.bg_length; i++) {
const bg_event_t& event = obj.bg_events[i];
const bool position_match = event.position.x == coord.x && event.position.y == coord.y;
if (position_match && event.sign_id != 0xff) {
const start_size_t& text = map.text_pointers[event.sign_id];
}
}
}
static uint32_t stats_page = 1;
static uint8_t level = 70;
void update()
{
state.player.tick();
change_maps();
update_warp();
/*
if (event::cursor_left() ) collision_move(maps[state.map], actor_t::left);
else if (event::cursor_right()) collision_move(maps[state.map], actor_t::right);
else if (event::cursor_up() ) collision_move(maps[state.map], actor_t::up);
else if (event::cursor_down() ) collision_move(maps[state.map], actor_t::down);
else if (event::button_a() ) check_sign();
*/
if (event::cursor_left() ) pokemon_raw_index = (pokemon_raw_index - 1);
else if (event::cursor_right()) pokemon_raw_index = (pokemon_raw_index + 1);
else if (event::cursor_up() ) level++;
else if (event::cursor_down() ) level--;
else if (event::button_a() ) stats_page = !stats_page;
if (pokemon_raw_index < 0) pokemon_raw_index = pokemon_t::count - 1;
else if (pokemon_raw_index >= pokemon_t::count) pokemon_raw_index = 0;
}
static uint32_t frame = 0;
void render()
{
frame++;
const offset_t offset = state.player.offset();
render_sprites(offset);
vdp1.reg.PTMR = PTMR__PTM__NOW;
vdp2.reg.SCXIN0 = offset.x;
vdp2.reg.SCXDN0 = 0;
vdp2.reg.SCYIN0 = offset.y;
vdp2.reg.SCYDN0 = 0;
render_map();
draw_menu(state.draw.base_pattern.font, fight_menu);
cursor_t cursor = { 1, 1 };
draw_menu_cursor(state.draw.base_pattern.font, fight_menu, cursor);
static pokemon_instance_t pokemon_instance;
pokemon_instance.species = static_cast<enum pokemon_t::pokemon>(pokemon_raw_index);
pokemon_instance.stat_experience = {0, 0, 0, 0, 0};
pokemon_instance.determinant_values.dvs = 0b1110'0101'1000'0110;
pokemon_instance.level = level;
pokemon_instance.determine_stats();
pokemon_instance.learn_all_moves();
if (frame % 4 == 0)
pokemon_instance.current_hit_points++;
if (pokemon_instance.current_hit_points > pokemon_instance.stat_values.hit_points)
pokemon_instance.current_hit_points = 0;
switch (stats_page) {
default:
case 0:
draw_stats1(state.draw.base_pattern.font, pokemon_instance);
break;
case 1:
draw_stats2(state.draw.base_pattern.font, pokemon_instance);
break;
}
}
extern "C"
void v_blank_in_int(void) __attribute__ ((interrupt_handler));
void v_blank_in_int()
{
scu.reg.IST &= ~(IST__V_BLANK_IN);
scu.reg.IMS = ~(IMS__SMPC | IMS__V_BLANK_IN);
sh2.reg.FRC.H = 0;
sh2.reg.FRC.L = 0;
sh2.reg.FTCSR = 0; // clear flags
render();
update();
// wait at least 300us, as specified in the SMPC manual.
// It appears reading FRC.H is mandatory and *must* occur before FRC.L on real
// hardware.
while ((sh2.reg.FTCSR & FTCSR__OVF) == 0 && sh2.reg.FRC.H == 0 && sh2.reg.FRC.L < 63);
// on real hardware, SF contains uninitialized garbage bits other than the
// lsb.
while ((smpc.reg.SF & 1) != 0);
smpc.reg.SF = 0;
smpc.reg.IREG[0].val = INTBACK__IREG0__STATUS_DISABLE;
smpc.reg.IREG[1].val = ( INTBACK__IREG1__PERIPHERAL_DATA_ENABLE
| INTBACK__IREG1__PORT2_15BYTE
| INTBACK__IREG1__PORT1_15BYTE
);
smpc.reg.IREG[2].val = INTBACK__IREG2__MAGIC;
smpc.reg.COMREG = COMREG__INTBACK;
}
extern "C"
void smpc_int(void) __attribute__ ((interrupt_handler));
void smpc_int(void)
{
scu.reg.IST &= ~(IST__SMPC);
scu.reg.IMS = ~(IMS__SMPC | IMS__V_BLANK_IN);
intback::fsm(digital_callback, nullptr);
}
void init_vdp1()
{
/* TVM settings must be performed from the second H-blank IN interrupt after the
V-blank IN interrupt to the H-blank IN interrupt immediately after the V-blank
OUT interrupt. */
// "normal" display resolution, 16 bits per pixel, 512x256 framebuffer
vdp1.reg.TVMR = TVMR__TVM__NORMAL;
// swap framebuffers every 1 cycle; non-interlace
vdp1.reg.FBCR = 0;
// during a framebuffer erase cycle, write the color "black" to each pixel
constexpr uint16_t black = 0x0000;
vdp1.reg.EWDR = black;
// the EWLR/EWRR macros use somewhat nontrivial math for the X coordinates
// erase upper-left coordinate
vdp1.reg.EWLR = EWLR__16BPP_X1(0) | EWLR__Y1(0);
// erase lower-right coordinate
vdp1.reg.EWRR = EWRR__16BPP_X3(319) | EWRR__Y3(239);
vdp1.vram.cmd[0].CTRL = CTRL__JP__JUMP_NEXT | CTRL__COMM__SYSTEM_CLIP_COORDINATES;
vdp1.vram.cmd[0].LINK = 0;
vdp1.vram.cmd[0].XC = 319;
vdp1.vram.cmd[0].YC = 239;
vdp1.vram.cmd[1].CTRL = CTRL__JP__JUMP_NEXT | CTRL__COMM__LOCAL_COORDINATE;
vdp1.vram.cmd[1].LINK = 0;
vdp1.vram.cmd[1].XA = 0;
vdp1.vram.cmd[1].YA = 0;
vdp1.vram.cmd[2].CTRL = CTRL__END;
// start drawing (execute the command list) on every frame
//vdp1.reg.PTMR = PTMR__PTM__FRAME_CHANGE;
}
void init_vdp2()
{
// sprite type
vdp2.reg.SPCTL = SPCTL__SPTYPE(0) // 2-bit priority
| SPCTL__SPCLMD; // enable RGB data from VDP1
vdp2.reg.PRISA = PRISA__S0PRIN(7) // Sprite register 0 PRIority Number
| PRISA__S1PRIN(5); // Sprite register 1 PRIority Number
vdp2.reg.PRINA = PRINA__N0PRIN(4)
| PRINA__N1PRIN(6);
// DISP: Please make sure to change this bit from 0 to 1 during V blank.
vdp2.reg.TVMD = ( TVMD__DISP | TVMD__LSMD__NON_INTERLACE
| TVMD__VRESO__240 | TVMD__HRESO__NORMAL_320);
vdp2.reg.EXTEN = 0;
/* set the color mode to 5bits per channel, 1024 colors */
vdp2.reg.RAMCTL = RAMCTL__CRKTE | RAMCTL__CRMD__RGB_5BIT_1024;// | RAMCTL__VRAMD | RAMCTL__VRBMD;
/* disable display of NBG0 */
vdp2.reg.BGON = 0;
/* set character format for NBG0 to palettized 16 color
set enable "cell format" for NBG0
set character size for NBG0 to 1x1 cell */
vdp2.reg.CHCTLA = CHCTLA__N0CHCN__16_COLOR
| CHCTLA__N0BMEN__CELL_FORMAT
| CHCTLA__N0CHSZ__1x1_CELL
| CHCTLA__N1CHCN__16_COLOR
| CHCTLA__N1BMEN__CELL_FORMAT
| CHCTLA__N1CHSZ__1x1_CELL;
/* plane size */
vdp2.reg.PLSZ = PLSZ__N0PLSZ__1x1
| PLSZ__N1PLSZ__1x1;
/* map plane offset
1-word: value of bit 6-0 * 0x2000
2-word: value of bit 5-0 * 0x4000
*/
constexpr int nbg0_plane = 0;
constexpr int nbg1_plane = 1;
//constexpr int plane_a_offset = plane_a * 0x2000;
//constexpr int page_size = 64 * 64 * 2; // N0PNB__1WORD (16-bit)
//constexpr int plane_size = page_size * 1;
// bits 8~6
vdp2.reg.MPOFN = MPOFN__N0MP(nbg0_plane >> 6)
| MPOFN__N1MP(nbg1_plane >> 6);
vdp2.reg.MPN0 = MPN0__N0MP(nbg0_plane);
vdp2.reg.MPN1 = MPN1__N1MP(nbg1_plane);
const uint32_t base_pattern = state.draw.base_pattern.font;
vdp2.reg.PNCN1 = PNCN1__N1PNB__1WORD | PNCN1__N1CNSM | PNCN1__N1SCN((base_pattern >> 10) & 0x1f);
const uint32_t value = ((base_pattern + 127) & 0xfff) | PATTERN_NAME_TABLE_1WORD__PALETTE(1);
fill<uint32_t>(&vdp2.vram.u32[0x2000 / 4], value | value << 16, 0x2000);
palette_data();
}
void main()
{
state.map = map_t::pallet_town;
//state.map = map_t::pewter_gym;
//state.map = map_t::viridian_forest;
//state.map = map_t::route_2;
state.player.world.x = 6;
state.player.world.y = 6;
load_vram();
v_blank_in();
init_vdp1();
init_vdp2();
constexpr uint16_t top_x = 80 - 1;
constexpr uint16_t top_y = 48 - 1;
constexpr uint16_t bot_x = 239 + 1;
constexpr uint16_t bot_y = 191 + 1;
vdp2.reg.WPSX0 = top_x << 1;
vdp2.reg.WPSY0 = top_y;
vdp2.reg.WPEX0 = bot_x << 1;
vdp2.reg.WPEY0 = bot_y;
vdp2.reg.WCTLA = WCTLA__N0W0E | WCTLA__N0W0A__OUTSIDE;
vdp2.reg.WCTLC = WCTLC__SPW0E | WCTLC__SPW0A__OUTSIDE;
// free-running timer
sh2.reg.TCR = TCR__CKS__INTERNAL_DIV128;
sh2.reg.FTCSR = 0;
// initialize smpc
smpc.reg.DDR1 = 0; // INPUT
smpc.reg.DDR2 = 0; // INPUT
smpc.reg.IOSEL = 0; // SMPC control
smpc.reg.EXLE = 0; //
sh2_vec[SCU_VEC__SMPC] = (u32)(&smpc_int);
sh2_vec[SCU_VEC__V_BLANK_IN] = (u32)(&v_blank_in_int);
scu.reg.IST = 0;
scu.reg.IMS = ~(IMS__SMPC | IMS__V_BLANK_IN);
}