home *** CD-ROM | disk | FTP | other *** search
- /* Ports.c: Z80 I/O - low level routines.
- *
- * Copyright 1996 Rui Fernando Ferreira Ribeiro.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #include "env.h"
- #include <dos.h>
-
-
- /* keeps last out to ula --- handy to know the border colour */
- static UCHAR out_ula = 0;
-
- /* count transition in beeper */
- static USHORT freq = 0;
-
- /* constants to count time to calculate freq */
- long TimeBeep = 0;
- long Tinterval = 0;
-
- /* returns colour of border */
- UCHAR get_sbrdr(void)
- {
- return out_ula & 7;
- }
-
- /* try to calculate frequency used */
- do_int_tasks()
- {
- if(Tinterval)
- {
- freq = ((USHORT)(70938L/Tinterval))*50;
- if((freq > 15000) || (freq < 200) )
- freq = 0;
- }
- else
- freq = 0;
- TimeBeep = Tinterval = 0;
- return freq;
- }
-
- /*=========================================================================*
- * writeport *
- *=========================================================================*/
- void writeport(port, value)
- USHORT port;
- UCHAR value;
- {
- /* ULA -- b4 - ALTF b3 - MIC b2b1b0 - BORDER
- */
-
- /* Even port - ULA */
- if(!(port & 1))
- {
- {
- /* delay of 1T when dealing with ULA */
- T(1);
-
- /***** Activate beeper *******/
- if(bSoundOn)
- outportb(0x61, (inportb(0x61) & 0xfc) | ((value & 0x10)?0:2) );
- /* Again sound for windows
- */
- #if defined(WINDOWS_SOUND)
- if(value & 0x10)
- {
- Tinterval = clock_ticks - TimeBeep;
- TimeBeep = clock_ticks;
- }
- #else
- /***** Activate beeper *******/
- if(bSoundOn)
- outportb(0x61, (inportb(0x61) & 0xfc) | ((value & 0x10)?0:2) );
- #endif
- out_ula = value;
- }
- }
- }
-
- /* buffer teclado -- se uma tecla for premida ficara 1 no respectivo bit */
- /* keyboard buffer -- if key pressed corresponding bit = 1 */
- UCHAR keybd_buff[8] = {0, 0, 0, 0, 0, 0, 0, 0};
-
- /* Spectrum keyboard :
- port high byte (low level active)
- multiples rows can be active at the same time
-
- Scan disposition:
-
- b3 b4
- b2 b5
- b1 b6
- b0 b7
-
- each scan line has 5 bits (5 keys, low level active):
-
- Left side: b0b1b2b3b4 Right side: b4b3b2b1b0
-
- When multiple scan lines are active, it will be made a 'and' operation between
- their values to get the final value ; ghost keys are generated with that
- operation
-
-
- KEYS:
-
- */
-
- UCHAR joystick = 0x00; /* FIRE UP DOWN RIGHT LEFT */
- /* 000 b4 b3 b2 b1 b0 */
-
-
- /*=========================================================================*
- * readport *
- *=========================================================================*/
- UCHAR readport(port)
- USHORT port;
- {
- UCHAR value = 0xff;
- static UCHAR lport;
-
- /* ULA -- b7 b6 b5 b4b3b2b1b0 */
- /* 1 1 EAR KEY_CODE */
-
-
-
- lport = ~(UCHAR)port;
- /* Joystick has precedence over keyboard -- Street Hawk, Command4
- Every major emulator got this wrong except Specem (aka Irish emulator)
- */
- if(lport & 0x20) /* joystick */
- value = joystick;
- else
- /* Port par - ULA */
- if(lport & 1)
- {
- UCHAR i = 0, tmp_value, t_val = 1;
- UCHAR scan_stat = ~(port >> 8);
-
- /* delay of 1T when dealing with ULA */
- T(1);
- value = 0;
- /* scan keyboard -- multiple rows can be active
- */
- for(i = 0 ; i < 8 ; i++)
- {
- if(t_val & scan_stat)
- {
- value |= keybd_buff[i];
- }
- t_val <<= 1;
- }
-
- /* Now it's time to test for ghost keys.
- */
- tmp_value = value;
- for(i = 0 ; i < 8 ; i++)
- {
- if(tmp_value & *(keybd_buff+i))
- value |= *(keybd_buff+i);
- }
- /* And finally we have the Spectrum value of port
- 0x40 is the difference between model 3 and model 2
- */
- value = (~value) & (bModel3?((UCHAR)0xBF):(UCHAR)0xFF);
- return value;
- }
- else
- /* Any other port --- this is not well implemented
- */
- if(clock_ticks < (USHORT)57121u)
- value = clock_ticks/224;
- else
- value = 0xFF;
- /* if(clock_ticks > (USHORT)14368u)
- {
- }*/
- return(value);
- }
-
- /* EOF: Ports.c */
-