Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
xrip committed Dec 12, 2023
1 parent eeec14a commit 30b519f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 75 deletions.
8 changes: 4 additions & 4 deletions drivers/vga-nextgen/vga.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ extern volatile bool manager_started;

#ifdef SOUND_SYSTEM
#include <hardware/pwm.h>
#define ZX_AY_PWM_PIN0 (26)
#define ZX_AY_PWM_PIN1 (27)
#define PWM_PIN0 (26)
#define PWM_PIN1 (27)
volatile uint32_t sound_cycles_per_vga = 0;

int16_t sn76489_sample();
Expand All @@ -101,8 +101,8 @@ bool __not_in_flash_func(sound_callback)(repeating_timer_t *rt) {
out += tickssource()(
#endif
out += sn76489_sample() >> 6;
pwm_set_gpio_level(ZX_AY_PWM_PIN0,(uint8_t)((uint16_t)out)); // Право
pwm_set_gpio_level(ZX_AY_PWM_PIN1,(uint8_t)((uint16_t)out)); // Лево
pwm_set_gpio_level(PWM_PIN0,(uint8_t)((uint16_t)out)); // Право
pwm_set_gpio_level(PWM_PIN1,(uint8_t)((uint16_t)out)); // Лево
return true;
}
#endif
Expand Down
100 changes: 33 additions & 67 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ extern "C" {
}

#if PICO_ON_DEVICE
#ifndef OVERCLOCKING
#define OVERCLOCKING 270
#endif

#include <pico/time.h>
#include <pico/multicore.h>
#include <hardware/pwm.h>
Expand All @@ -24,6 +20,7 @@ extern "C" {
#include "ps2.h"
#include "usb.h"
}

#else
#define SDL_MAIN_HANDLED

Expand All @@ -41,109 +38,70 @@ uint32_t DIRECT_RAM_BORDER = PSRAM_AVAILABLE ? RAM_SIZE : (SD_CARD_AVAILABLE ? R
bool runing = true;

#if PICO_ON_DEVICE
repeating_timer_t sound_timer;
#define ZX_AY_PWM_PIN0 (26)
#define ZX_AY_PWM_PIN1 (27)
pwm_config config = pwm_get_default_config();
#define PWM_PIN0 (26)
#define PWM_PIN1 (27)

void PWM_init_pin(uint pinN){
void PWM_init_pin(uint8_t pinN) {
gpio_set_function(pinN, GPIO_FUNC_PWM);
uint slice_num = pwm_gpio_to_slice_num(pinN);

pwm_config c_pwm = pwm_get_default_config();
pwm_config_set_clkdiv(&c_pwm,1.0);
pwm_config_set_wrap(&c_pwm,255);//MAX PWM value
pwm_init(slice_num,&c_pwm,true);
pwm_config_set_clkdiv(&config, 1.0);
pwm_config_set_wrap(&config, 255); //MAX PWM value
pwm_init(pwm_gpio_to_slice_num(pinN), &config, true);
}

struct semaphore vga_start_semaphore;
/* Renderer loop on Pico's second core */
void __time_critical_func(render_core)() {
// create an alarm pool on core 1
// alarm_pool_t *core1pool = alarm_pool_create(2, 16) ;
// Create a repeating timer that calls repeating_timer_callback.
struct repeating_timer timer_core_1;

gpio_set_function(BEEPER_PIN, GPIO_FUNC_PWM);
pwm_init(pwm_gpio_to_slice_num(BEEPER_PIN), &config, true);

PWM_init_pin(ZX_AY_PWM_PIN0);
PWM_init_pin(ZX_AY_PWM_PIN1);
#ifdef SOUND_SYSTEM
#if PICO_ON_DEVICE
pwm_config config = pwm_get_default_config();
static const int sound_frequency = 11000;
#endif
// if (!alarm_pool_add_repeating_timer_us(core1pool, -1000000 / sound_frequency, sound_callback, NULL, &sound_timer)) {
// logMsg("Failed to add timer");
// sleep_ms(3000);
// }
#endif
graphics_init();
graphics_set_buffer(VIDEORAM, 320, 200);
graphics_set_textbuffer(VIDEORAM+32768);
graphics_set_textbuffer(VIDEORAM + 32768);
graphics_set_bgcolor(0);
graphics_set_offset(0, 0);
graphics_set_flashmode(true, true);
/*

for (int i = 0; i < 16; ++i) {
graphics_set_palette(i, cga_palette[i]);
}
*/


for (int i = 0; i < 16; ++i) {
graphics_set_palette(i, cga_palette[i]);
}


sem_acquire_blocking(&vga_start_semaphore);
uint8_t tick50ms = 0;

uint8_t tick50ms_counter = 0;
while (true) {
doirq(0);
busy_wait_us(timer_period);
if (tick50ms % 2 == 0 && nespad_available) {
if (tick50ms_counter % 2 == 0 && nespad_available) {
nespad_read();
if (nespad_state) {
//logMsg("TEST");
// TODO: Speedup in time
sermouseevent(nespad_state & DPAD_B ? 1 : nespad_state & DPAD_A ? 2 : 0, nespad_state & DPAD_LEFT ? -3 : nespad_state & DPAD_RIGHT ? 3 : 0, nespad_state & DPAD_UP ? -3 : nespad_state & DPAD_DOWN ? 3 : 0);
sermouseevent(nespad_state & DPAD_B ? 1 : nespad_state & DPAD_A ? 2 : 0,
nespad_state & DPAD_LEFT ? -3 : nespad_state & DPAD_RIGHT ? 3 : 0,
nespad_state & DPAD_UP ? -3 : nespad_state & DPAD_DOWN ? 3 : 0);
}
}
if (tick50ms == 0 || tick50ms == 10) {
if (tick50ms_counter == 0 || tick50ms_counter == 10) {
cursor_blink_state ^= 1;
}
if (tick50ms < 20) {
tick50ms++;
if (tick50ms_counter < 20) {
tick50ms_counter++;
}
else {
tick50ms = 0;
tick50ms_counter = 0;
}
}
}

#else


static int RendererThread(void* ptr) {
while (runing) {
exec86(2000);
#if !PICO_ON_DEVICE
//SDL_Delay(1);
#endif
}
return 0;
}

#endif

extern "C" {
int16_t sn76489_sample();
}

static void fill_audio(void* udata, uint8_t* stream, int len) { // for SDL mode only
// int16_t out = adlibgensample() >> 4;
//

int16_t out = 0;
#if SOUND_BLASTER || ADLIB
out += (adlibgensample() >> 3);
Expand All @@ -160,7 +118,7 @@ static void fill_audio(void* udata, uint8_t* stream, int len) { // for SDL mode
*stream = (uint8_t)((uint16_t)out);
//memcpy(stream, &out, len);
}

#endif


int main() {
Expand All @@ -177,6 +135,14 @@ int main() {

//stdio_init_all();

gpio_set_function(BEEPER_PIN, GPIO_FUNC_PWM);
pwm_init(pwm_gpio_to_slice_num(BEEPER_PIN), &config, true);

#ifdef SOUND_SYSTEM
PWM_init_pin(PWM_PIN0);
PWM_init_pin(PWM_PIN1);
#endif

gpio_init(PICO_DEFAULT_LED_PIN);
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);

Expand All @@ -199,18 +165,18 @@ int main() {
graphics_set_mode(TEXTMODE_80x30);

init_psram();
DIRECT_RAM_BORDER = PSRAM_AVAILABLE ? RAM_SIZE : (SD_CARD_AVAILABLE ? RAM_PAGE_SIZE : RAM_SIZE);

FRESULT result = f_mount(&fs, "", 1);
if (result != FR_OK) {
if (FR_OK != result) {
char tmp[80];
sprintf(tmp, "Unable to mount SD-card: %s (%d)", FRESULT_str(result), result);
logMsg(tmp);
} else {
SD_CARD_AVAILABLE = true;
DIRECT_RAM_BORDER = PSRAM_AVAILABLE ? RAM_SIZE : (SD_CARD_AVAILABLE ? RAM_PAGE_SIZE : RAM_SIZE);
}

DIRECT_RAM_BORDER = PSRAM_AVAILABLE ? RAM_SIZE : (SD_CARD_AVAILABLE ? RAM_PAGE_SIZE : RAM_SIZE);

if (!PSRAM_AVAILABLE && !SD_CARD_AVAILABLE) {
logMsg((char *)"Mo PSRAM or SD CARD available. Only 160Kb RAM will be usable...");
sleep_ms(3000);
Expand Down Expand Up @@ -762,7 +728,7 @@ int main() {
SDL_BlitScaled(drawsurface, NULL, screen, NULL);
SDL_UpdateWindowSurface(window);
#else
exec86(200);
exec86(2000);
if_manager();
if_swap_drives();
if_overclock();
Expand Down
8 changes: 4 additions & 4 deletions src/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static void swap_drives(uint8_t cmd) {
swap_drive_message();
}

void if_swap_drives() {
inline void if_swap_drives() {
if (backspacePressed && tabPressed && ctrlPressed) {
swap_drives(8);
}
Expand Down Expand Up @@ -587,15 +587,15 @@ bool handleScancode(uint32_t ps2scancode) { // core 1
return manager_started;
}

int overclock() {
inline int overclock() {
if (tabPressed && ctrlPressed) {
if (plusPressed) return 1;
if (minusPressed) return -1;
}
return 0;
}

void if_manager() {
inline void if_manager() {
if (manager_started) {
return;
}
Expand All @@ -608,7 +608,7 @@ void if_manager() {

uint32_t overcloking_khz = OVERCLOCKING * 1000;

void if_overclock() {
inline void if_overclock() {
int oc = overclock();
if (oc > 0) overcloking_khz += 1000;
if (oc < 0) overcloking_khz -= 1000;
Expand Down

0 comments on commit 30b519f

Please sign in to comment.