File:  [UNIX Amiga Emulator] / uae / src / bebox.cpp
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Tue Apr 24 16:39:06 2018 UTC (8 years, 2 months ago) by root
Branches: MAIN, BSchmidt
CVS tags: uae068, HEAD
uae-0.6.8

/* 
 * UAE - The Un*x Amiga Emulator
 *
 * BeBox port specific stuff
 *
 * (c) 1996-1997 Christian Bauer
 * (c) 1996 Patrick Hanevold
 */

#include <AppKit.h>
#include <InterfaceKit.h>
#include <KernelKit.h>
#include <MediaKit.h>
#include <Record.h>
#include <File.h>
#include <device/Joystick.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#define __STDC__ 1
#define __GNU_LIBRARY__
#include <getopt.h>
#undef __GNU_LIBRARY__
#undef __STDC__

#include "bebox.h"


// Messages
const ulong MSG_INSERT_DF0 = 'idf0';
const ulong MSG_EJECT_DF0 = 'edf0';
const ulong MSG_INSERT_DF1 = 'idf1';
const ulong MSG_EJECT_DF1 = 'edf1';
const ulong MSG_INSERT_DF2 = 'idf2';
const ulong MSG_EJECT_DF2 = 'edf2';
const ulong MSG_INSERT_DF3 = 'idf3';
const ulong MSG_EJECT_DF3 = 'edf3';
const ulong MSG_FR1 = 'mfr1';
const ulong MSG_FR2 = 'mfr2';
const ulong MSG_FR3 = 'mfr3';
const ulong MSG_FR4 = 'mfr4';
const ulong MSG_FR5 = 'mfr5';
const ulong MSG_FR6 = 'mfr6';
const ulong MSG_FR7 = 'mfr7';
const ulong MSG_RESET = 'rset';
const ulong MSG_DEBUG = 'dbug';
const ulong MSG_REDRAW = 'draw';


// LED colors
const rgb_color PowerDark = {0x40, 0x00, 0x00, 0};
const rgb_color PowerLight = {0xff, 0x00, 0x00, 0};
const rgb_color DriveDark = {0x00, 0x40, 0x00, 0};
const rgb_color DriveLight = {0x00, 0xff, 0x00, 0};


// Variables
static UAE *the_app;
static BitmapView *bitmap_view;
static UAEWindow *bitmap_window;
static bool reset_thyself = FALSE;

static key_info old_key_info;
static bool window_open;
static bool LEDs[4];

static int the_argc;
static char **the_argv;

static BJoystick *joy;


// Array for converting Be keycodes to Amiga keycodes
int keycode2amiga[128] = {
	-1, AK_ESC, AK_F1, AK_F2, AK_F3, AK_F4, AK_F5, AK_F6,
	AK_F7, AK_F8, AK_F9, AK_F10, -1, AK_mousestuff, -1, -1,

	-1, AK_BACKQUOTE, AK_1, AK_2, AK_3, AK_4, AK_5, AK_6,
	AK_7, AK_8, AK_9, AK_0, AK_MINUS, AK_EQUAL, AK_BS, AK_HELP,

	AK_NPLPAREN, AK_NPRPAREN, -1, AK_NPDIV, AK_NPMUL, AK_NPSUB, AK_TAB, AK_Q,
	AK_W, AK_E, AK_R, AK_T, AK_Y, AK_U, AK_I, AK_O,

	AK_P, AK_LBRACKET, AK_RBRACKET, AK_BACKSLASH, AK_DEL, AK_LALT, AK_RALT, AK_NP7,
	AK_NP8, AK_NP9, AK_NPADD, AK_CAPSLOCK, AK_A, AK_S, AK_D, AK_F,

	AK_G, AK_H, AK_J, AK_K, AK_L, AK_SEMICOLON, AK_QUOTE, AK_RET,
	AK_NP4, AK_NP5, AK_NP6, AK_LSH, AK_Z, AK_X, AK_C, AK_V,

	AK_B, AK_N, AK_M, AK_COMMA, AK_PERIOD, AK_SLASH, AK_RSH, AK_UP,
	AK_NP1, AK_NP2, AK_NP3, AK_ENT, AK_CTRL, AK_LAMI, AK_SPC, AK_RAMI,

	AK_RALT, AK_LF, AK_DN, AK_RT, AK_NP0, AK_NPDEL, AK_LALT, AK_RALT,
	AK_LTGT, -1, -1, -1, -1, -1, -1, -1,

	-1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1
};


/*
 *  Create application object and start it
 */

main()
{
	the_app = new UAE();
	the_app->Run();
	delete the_app;
	
	return 0;
}


/*
 *  UAE Constructor: Initialize member variables
 */

UAE::UAE() : BApplication('UAEm')
{
	the_bitmap = NULL;
	main_window = NULL;
	window_open = FALSE;
}


/*
 *  Copy command line arguments
 */

void UAE::ArgvReceived(int argc, char **argv)
{
	the_argc = argc;
	the_argv = new char *[argc];
	for (int i=0; i<argc; i++)
		the_argv[i] = strdup(argv[i]);
}


/*
 *  Arguments processed, create and start emulation
 */

void UAE::ReadyToRun(void)
{
	// Start the emulation thread
	the_thread = spawn_thread(thread_func, "UAE 68000", B_NORMAL_PRIORITY, this);
	resume_thread(the_thread);
}


/*
 *  Quit requested (either by menu or by closing the window)
 */

bool UAE::QuitRequested(void)
{
	// Quit the thread
	long l;
	quit_program = 1;
	regs.spcflags |= SPCFLAG_BRK;
	wait_for_thread(the_thread, &l);

	return TRUE;
}


/*
 *  The thread's main function
 */

long UAE::thread_func(void *obj)
{
	real_main(the_argc, the_argv);
	close_sound();
	return 0;
}


/*
 *  Display "about" window
 */

void UAE::AboutRequested(void)
{
	char str[256];

	sprintf(str, "     Un*x Amiga Emulator V%d.%d.%d\n"
		     	"          by Bernd Schmidt\n"
		     	"    BeBox port by Christian Bauer\n"
		     	"Additional porting by Patrick Hanevold", UAEMAJOR, UAEMINOR, UAEURSAMINOR);
	BAlert *the_alert = new BAlert("", str, "OK");
	the_alert->Go();
}


/*
 *  Run file panel for floppy disk image selection
 */

void UAEWindow::request_floppy(char *title, int drive_num)
{
	BMessage *sendmsg = new BMessage(B_REFS_RECEIVED);
	sendmsg->AddLong("uae_drive_num", drive_num);
	be_app->RunFilePanel(title, "Insert", NULL, FALSE, sendmsg);
}


/*
 *  File panel reply
 */

void UAE::RefsReceived(BMessage *msg)
{
	ulong type;
	long count;
	char path[512];

	// Find drive number (default is 0)
	int drive_num = msg->FindLong("uae_drive_num");

	// Find path of file
	msg->GetInfo("refs", &type, &count);
	for (int i=--count; i>=0; i--) {
		record_ref item = msg->FindRef("refs", i);
		if (item.database >= 0 && item.record >= 0 && does_ref_conform(item, "File")) {
			BFile *file = new BFile;
			file->SetRef(item);
			file->GetPath(path, 511);

			// Eject old disk, then insert new one
			disk_eject(drive_num);
			disk_insert(drive_num, path);
			delete file;
		}
	}
}


/*
 *  UAE Window constructor
 */

LEDView *PowerLED, *DriveLED[4];

UAEWindow::UAEWindow(BRect frame, BBitmap *bitmap) : BWindow(frame,"UAE", B_TITLED_WINDOW, B_NOT_RESIZABLE|B_NOT_ZOOMABLE)
{
	int i;

	// Move window to right position
	Lock();
	MoveTo(80, 80);

	// Set up menus
	BMenuBar *MenuBar = new BMenuBar(Bounds(), "", B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP, B_ITEMS_IN_ROW, TRUE);

	BMenu *Menu;
	BMenu *SubMenu;
	BMenuItem *RateItem[7];

	Menu = new BMenu("Amiga"); MenuBar->AddItem(Menu);
		SubMenu = new BMenu("Framerate"); Menu->AddItem(SubMenu);
			SubMenu->SetRadioMode(TRUE);
			RateItem[0] = new BMenuItem("1/1", new BMessage(MSG_FR1)); SubMenu->AddItem(RateItem[0]);
			RateItem[1] = new BMenuItem("1/2", new BMessage(MSG_FR2)); SubMenu->AddItem(RateItem[1]);
			RateItem[2] = new BMenuItem("1/3", new BMessage(MSG_FR3)); SubMenu->AddItem(RateItem[2]);
			RateItem[3] = new BMenuItem("1/4", new BMessage(MSG_FR4)); SubMenu->AddItem(RateItem[3]);
			RateItem[4] = new BMenuItem("1/5", new BMessage(MSG_FR5)); SubMenu->AddItem(RateItem[4]);
			RateItem[5] = new BMenuItem("1/6", new BMessage(MSG_FR6)); SubMenu->AddItem(RateItem[5]);
			RateItem[6] = new BMenuItem("1/7", new BMessage(MSG_FR7)); SubMenu->AddItem(RateItem[6]);
		Menu->AddItem(new BMenuItem("Reset", new BMessage(MSG_RESET)));
		Menu->AddItem(new BMenuItem("Debugger", new BMessage(MSG_DEBUG)));
	Menu = new BMenu("Floppy"); MenuBar->AddItem(Menu);
		SubMenu = new BMenu("DF0:"); Menu->AddItem(SubMenu);
			SubMenu->AddItem(new BMenuItem("Insert...", new BMessage(MSG_INSERT_DF0)));
			SubMenu->AddItem(new BMenuItem("Eject", new BMessage(MSG_EJECT_DF0)));
		SubMenu = new BMenu("DF1:"); Menu->AddItem(SubMenu);
			SubMenu->AddItem(new BMenuItem("Insert...", new BMessage(MSG_INSERT_DF1)));
			SubMenu->AddItem(new BMenuItem("Eject", new BMessage(MSG_EJECT_DF1)));
		SubMenu = new BMenu("DF2:"); Menu->AddItem(SubMenu);
			SubMenu->AddItem(new BMenuItem("Insert...", new BMessage(MSG_INSERT_DF2)));
			SubMenu->AddItem(new BMenuItem("Eject", new BMessage(MSG_EJECT_DF2)));
		SubMenu = new BMenu("DF3:"); Menu->AddItem(SubMenu);
			SubMenu->AddItem(new BMenuItem("Insert...", new BMessage(MSG_INSERT_DF3)));
			SubMenu->AddItem(new BMenuItem("Eject", new BMessage(MSG_EJECT_DF3)));
	RateItem[0]->SetMarked(TRUE);
	AddChild(MenuBar);

	// Resize window to fit menu bar
	MenuBar->ResizeBy(-5*4, 0);	// Make room for LEDs
	BRect MenuRect = MenuBar->Frame();
	ResizeBy(0, MenuRect.bottom + 1);
	SetDiscipline(FALSE);

	// Create LEDs
	BRect PowerRect = Bounds();
	PowerRect.bottom = MenuRect.bottom;
	PowerRect.left = MenuRect.right+1;
	PowerRect.right = PowerRect.left+3;
	PowerLED = new LEDView(PowerRect, PowerLight, PowerDark);
	AddChild(PowerLED);

	BRect DriveRect = PowerRect;
	for (i=0; i<4; i++) {
		DriveRect.left += 4;
		DriveRect.right += 4;
		DriveLED[i] = new LEDView(DriveRect, DriveLight, DriveDark);
		AddChild(DriveLED[i]);
	}

	// Create bitmap view
	BRect BitmapRect = frame;
	BitmapRect.OffsetTo(0, MenuRect.bottom + 1);
	main_view = new BitmapView(BitmapRect, bitmap);
	AddChild(main_view);
	main_view->MakeFocus();
	main_view->GetKeys(&old_key_info, FALSE);

	bitmap_view = main_view;
	bitmap_window = this;

	// Initialize xcolors
	i = 0;
	for (int r=0; r<16; r++) {
		for (int g=0; g<16; g++) {
			for (int b=0; b<16; b++)
				xcolors[i++] = index_for_color(r<<4 | r, g<<4 | g, b<<4 | b);
		}
	}

	// Show window
	Unlock();
	Show();
	window_open = TRUE;
}


/*
 *  Message received
 */

void UAEWindow::MessageReceived(BMessage *msg)
{
	BMessage *msg2;

	switch(msg->what) {
		case MSG_INSERT_DF0:
			request_floppy("Insert floppy in DF0:", 0);
			break;
		case MSG_EJECT_DF0:
			disk_eject(0);
			break;
		case MSG_INSERT_DF1:
			request_floppy("Insert floppy in DF1:", 1);
			break;
		case MSG_EJECT_DF1:
			disk_eject(1);
			break;
		case MSG_INSERT_DF2:
			request_floppy("Insert floppy in DF2:", 2);
			break;
		case MSG_EJECT_DF2:
			disk_eject(2);
			break;
		case MSG_INSERT_DF3:
			request_floppy("Insert floppy in DF3:", 3);
			break;
		case MSG_EJECT_DF3:
			disk_eject(3);
			break;

		case MSG_FR1:
			framerate = 1;
			break;
		case MSG_FR2:
			framerate = 2;
			break;
		case MSG_FR3:
			framerate = 3;
			break;
		case MSG_FR4:
			framerate = 4;
			break;
		case MSG_FR5:
			framerate = 5;
			break;
		case MSG_FR6:
			framerate = 6;
			break;
		case MSG_FR7:
			framerate = 7;
			break;

		case MSG_RESET:
			reset_thyself = TRUE;
			break;

		case MSG_DEBUG:
			activate_debugger();
			break;

		case MSG_REDRAW:
			MessageQueue()->Lock();
			while ((msg2 = MessageQueue()->FindMessage(MSG_REDRAW, 0)) != NULL)
				MessageQueue()->RemoveMessage(msg2);
			MessageQueue()->Unlock();
			Lock();
			main_view->Draw(BRect(0, 0, gfx_requested_width-1, gfx_requested_height-1));
			Unlock();
			break;

		default:
			BWindow::MessageReceived(msg);
			break;
	}
}


/*
 *  Closing the window quits UAE
 */

bool UAEWindow::QuitRequested(void)
{
	window_open = FALSE;
	be_app->PostMessage(B_QUIT_REQUESTED);
	return FALSE;
}


/*
 *  Bitmap view constructor
 */

BitmapView::BitmapView(BRect frame, BBitmap *bitmap) : BView(frame, "", B_FOLLOW_NONE, B_WILL_DRAW)
{
	the_bitmap = bitmap;
}


/*
 *  Blit the bitmap
 */

void BitmapView::Draw(BRect update)
{
	DrawBitmap(the_bitmap, update, update);
}

void BitmapView::Draw(BRect from, BRect to)
{
	DrawBitmap(the_bitmap, from, to);
}


/*
 *  Key pressed
 */

void BitmapView::KeyDown(ulong aChar)
{
	// Break key jumps into debugger
	if (aChar == B_FUNCTION_KEY && Window()->CurrentMessage()->FindLong("key") == B_PAUSE_KEY)
		activate_debugger();
}


/*
 *  Mouse moved
 */

void BitmapView::MouseMoved(BPoint point, ulong transit, BMessage *message)
{
	newmousecounters = 0;
	switch (transit) {
		case B_ENTERED_VIEW:
			newmousecounters = 1;
			lastmx = point.x;
			lastmy = point.y;
			break;
		case B_EXITED_VIEW:
			break;
		case B_INSIDE_VIEW:
			lastmx = point.x;
			lastmy = point.y;
			break;
	}
}


/*
 *  LED
 */

LEDView::LEDView(BRect frame, rgb_color active, rgb_color idle) : BView(frame, "", B_FOLLOW_NONE, B_WILL_DRAW)
{
	active_color = active;
	idle_color = idle;
	state = FALSE;
	SetViewColor(idle_color);
	SetHighColor(idle_color);
	bounds = Bounds();
}

void LEDView::Draw(BRect update)
{
	FillRect(bounds);
}

void LEDView::SetState(bool new_state)
{
	if (new_state != state) {
		state = new_state;
		Window()->Lock();
		if (state) {
			SetViewColor(active_color);
			SetHighColor(active_color);
		} else {
			SetViewColor(idle_color);
			SetHighColor(idle_color);
		}
		Draw(bounds);
		Window()->Unlock();
	}
}


/*
 *  Redraw a line
 */

void flush_line(int y)
{
	if (window_open) {
		bitmap_window->Lock();
		bitmap_view->Draw(BRect(0, y, gfx_requested_width-1, y));
		bitmap_window->Unlock();
	}
}


/*
 *  Redraw a block
 */

void flush_block(int ystart, int ystop)
{
}


/*
 *  Redraw the screen
 */

void flush_screen(int ystart, int ystop)
{
	if (window_open)
		bitmap_window->PostMessage(new BMessage(MSG_REDRAW));
}


/*
 *  Init graphics
 */

int graphics_init(void)
{
	return the_app->GraphicsInit();
}

int UAE::GraphicsInit(void)
{
	if (gfx_requested_width < 320)
		gfx_requested_width = 320;
	if (gfx_requested_height < 200)
		gfx_requested_height = 200;

	gfxvidinfo.maxlinetoscr = gfx_requested_width;
	gfxvidinfo.maxline = gfx_requested_height;

	// Allocate bitmap
	the_bitmap = new BBitmap(BRect(0, 0, gfx_requested_width-1, gfx_requested_height-1), B_COLOR_8_BIT);

	// Set up vidinfo
	gfxvidinfo.pixbytes = 1;
	gfxvidinfo.rowbytes = the_bitmap->BytesPerRow();
	gfxvidinfo.bufmem = (char *)the_bitmap->Bits();
	gfxvidinfo.maxblocklines = 100; /* whatever... */

	// Open window
	main_window = new UAEWindow(BRect(0, 0, gfx_requested_width-1, gfx_requested_height-1), the_bitmap);

	// Initialize mouse and keyboard variables
	buttonstate[0] = buttonstate[1] = buttonstate[2] = 0;
	lastmx = lastmy = 0;
	newmousecounters = 0;

	return TRUE;
}


/*
 * Exit graphics
 */

void graphics_leave(void)
{
	the_app->GraphicsLeave();
}

void UAE::GraphicsLeave(void)
{
	// Deallocate bitmap
	delete the_bitmap;
}


/*
 *  Poll mouse and keyboard
 */

void handle_events(void)
{
	key_info the_key_info;
	int be_code, be_byte, be_bit, amiga_code;
	BPoint mouse_point;
	ulong mouse_buttons;

	if (reset_thyself) {
		m68k_reset();
		reset_thyself = FALSE;
	}

	// Redraw drive LEDs
	for (int i=0; i<4; i++)
		DriveLED[i]->SetState(LEDs[i]);

	if (window_open && bitmap_window->IsActive()) {

		bitmap_window->Lock();
		bitmap_view->GetKeys(&the_key_info, FALSE);
		bitmap_view->GetMouse(&mouse_point, &mouse_buttons, FALSE);
		bitmap_window->Unlock();

		// Keyboard
		if (memcmp(the_key_info.key_states, old_key_info.key_states, sizeof(the_key_info.key_states))) {
			for (be_code=0; be_code<0x80; be_code++) {
				be_byte = be_code >> 3;
				be_bit = 1 << (~be_code & 7);

				// Key state changed?
				if ((the_key_info.key_states[be_byte] & be_bit)
				     != (old_key_info.key_states[be_byte] & be_bit)) {

					amiga_code = keycode2amiga[be_code];
					if (the_key_info.key_states[be_byte] & be_bit) {

						// Key pressed
						if (amiga_code == AK_mousestuff)
							togglemouse();
						else
							record_key(amiga_code << 1);
					} else {

						// Key released
						record_key((amiga_code << 1) | 1);
					}
				}
			}		
			old_key_info = the_key_info;
		}

		// "Affengriff"
		if ((the_key_info.key_states[0x5c >> 3] & (1 << (~0x5c & 7)))
		 && (the_key_info.key_states[0x5d >> 3] & (1 << (~0x5d & 7)))
		 && (the_key_info.key_states[0x5f >> 3] & (1 << (~0x5f & 7))))
			m68k_reset();

		// Scroll lock toggles inhibit_frame
		inhibit_frame = the_key_info.key_states[0x0f >> 3] & (1 << (~0x0f & 7));

		// Mouse buttons
		if (mouse_point.x >= 0 && mouse_point.y >= 0 && mouse_point.x < gfx_requested_width && mouse_point.y < gfx_requested_height) {
			buttonstate[0] = mouse_buttons & B_PRIMARY_MOUSE_BUTTON;
			buttonstate[1] = mouse_buttons & B_TERTIARY_MOUSE_BUTTON;
			buttonstate[2] = mouse_buttons & B_SECONDARY_MOUSE_BUTTON;
		}
	}
}


/*
 *  Joystick routines
 */

void read_joystick(UWORD *dir, int *button)
{
	static int joy_minx = 32767, joy_maxx = 0,
			   joy_miny = 32767, joy_maxy = 0;
	int left = 0, right = 0, top = 0, bot = 0;

	*dir = 0;
	*button = 0;

	if (joy->Update() != B_ERROR) {
		if (joy->horizontal > joy_maxx)
			joy_maxx = joy->horizontal;
		if (joy->horizontal < joy_minx)
			joy_minx = joy->horizontal;
		if (joy->vertical > joy_maxy)
			joy_maxy = joy->vertical;
		if (joy->vertical < joy_miny)
			joy_miny = joy->vertical;

		if (joy_maxx-joy_minx < 100 || joy_maxy-joy_miny < 100)
			return;

		if (joy->horizontal < (joy_minx + (joy_maxx-joy_minx)/3))
			right = 1;
		else if (joy->horizontal > (joy_minx + 2*(joy_maxx-joy_minx)/3))
			left = 1;

		if (joy->vertical < (joy_miny + (joy_maxy-joy_miny)/3))
			bot = 1;
		else if (joy->vertical > (joy_miny + 2*(joy_maxy-joy_miny)/3))
			top = 1;

		if (left) top = !top;
		if (right) bot = !bot;
	    *dir = bot | (right << 1) | (top << 8) | (left << 9);
	    *button = !joy->button1;
	}
}

void init_joystick(void)
{
	joy = new BJoystick();
	joy->Open("joystick1");
}

void close_joystick(void)
{
	joy->Close();
	delete joy;
}


/*
 *  Sound routines
 */

#ifndef DONT_WANT_SOUND
extern "C" {
extern UWORD *sndbuffer;
extern UWORD *bufpt;
extern int sound_available;
extern int smplcnt;
extern int sndbufsize;
extern int init_sound(void);
extern void flush_sound_buffer(void);
extern void init_sound_table8(void);
extern void init_sound_table16(void);
extern void sample8_handler(void);
extern void sample16_handler(void);
};

static UBYTE *buffers[2] = {NULL, NULL};
static int buf_num;
static BAudioSubscriber *the_sub;
static bool sound_ready = FALSE;
static sem_id sound_sync_sem;

bool stream_func(void *arg, char *buf, long count);

int init_sound(void)
{
	sound_sync_sem = create_sem(0, "UAE Sound Sync Semaphore");
	the_sub = new BAudioSubscriber("UAE DAC subscriber");

	if (!produce_sound)
		return 0;
	sound_ready = the_sub->Subscribe(B_DAC_STREAM, the_sub->ID(), FALSE) == B_NO_ERROR;
	if (!sound_ready)
		return 0;

	if (sound_desired_bsiz == 0)
		sound_desired_bsiz = sound_desired_freq / 50 * 2;
	sndbufsize = sound_desired_bsiz;
	buffers[0] = new UBYTE[sndbufsize];
	buffers[1] = new UBYTE[sndbufsize];
	memset(buffers[0], 0, sndbufsize);
	memset(buffers[1], 0, sndbufsize);
	buf_num = 0;
	sndbufpt = sndbuffer = (UWORD *)buffers[buf_num];

	eventtab[ev_sample].evtime = (long)maxhpos * maxvpos * 50 / sound_desired_freq;
	init_sound_table16();
	eventtab[ev_sample].handler = sample16_handler;

	sound_available = 1;

	the_sub->SetDACSampleInfo(2, 1, B_BIG_ENDIAN, B_LINEAR_SAMPLES);
	the_sub->SetSamplingRate(sound_desired_freq);
	the_sub->EnterStream(NULL, TRUE, NULL, stream_func, NULL, TRUE);
	the_sub->SetStreamBuffers(sound_desired_bsiz, 4);
    return 1;
}

void close_sound(void)
{
	if (sound_ready) {
		the_sub->ExitStream(TRUE);
		the_sub->SetStreamBuffers(4096, 8);
		the_sub->Unsubscribe();
		sound_ready = FALSE;
	}
	delete the_sub;

	delete_sem(sound_sync_sem);

	delete buffers[0];
	delete buffers[1];
}

void flush_sound_buffer(void)
{
	if (sound_ready) {
		long l;
		get_sem_count(sound_sync_sem, &l);
		if (l > 0)
			acquire_sem_etc(sound_sync_sem, l+1, 0, 0);
		else
			acquire_sem(sound_sync_sem);
	}

	sndbufpt = sndbuffer = (UWORD *)buffers[buf_num];
	buf_num ^= 1;
}

bool stream_func(void *arg, char *buf, long count)
{
	memcpy(buf, buffers[buf_num], count);
	release_sem(sound_sync_sem);
	return TRUE;
}
#else
int init_sound(void)
{
	produce_sound = 0;
	return 0;
}

void close_sound(void)
{
}
#endif


/*
 *  Misc routines
 */

int debuggable(void)
{
    return TRUE;
}

int needmousehack(void)
{
    return TRUE;
}

void LED(int on)
{
	PowerLED->SetState(!on);
}

void target_specific_usage(void)
{
	printf("  -S n                     : Sound emulation accuracy (n = 0, 1, 2 or 3)\n"
		   "                             For sound emulation, n = 2 is recommended\n");
	printf("  -R n                     : Use n Hz to output sound. Common values are\n"
		   "                             22050 Hz or 44100 Hz\n");
	printf("  -B n                     : Use a sound buffer of n bytes (use small\n"
		   "                             values on fast machines)\n");
}

void setup_brkhandler(void)
{
}

void gui_led(int led, int on)
{
	if (led > 0 && led < 5)
		LEDs[led-1] = on;
}

int gui_init(void)
{
	LEDs[0] = LEDs[1] = LEDs[2] = LEDs[3] = FALSE;
	quit_program = 0;
	return 0;
}

void gui_exit(void)
{
}

int gui_update(void)
{
	return 0;
}

void gui_filename(int num, const char *name)
{
}

static void getline(char *p)
{
}

void gui_handle_events(void)
{
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.