Previous 15994 Revisions Next

r11474 Saturday 7th May, 2011 at 12:04:36 UTC by Miodrag Milanović
Put some things back in MESS since they do not belong yet into emu section. (no whatsnew)
[src/emu]emu.mak
[src/emu/machine]msm8251.c msm8251.h serial.c serial.h upd765.c upd765.h
[src/mame/drivers]sg1000.c
[src/mame/includes]sg1000.h
[src/mess]mess.mak
[src/mess/drivers]sg1000.c
[src/mess/machine]msm8251.c* msm8251.h* serial.c* serial.h* upd765.c* upd765.h*

src/mame/includes/sg1000.h
r11473r11474
22#define __SG1000__
33
44#include "machine/ram.h"
5#include "video/tms9928a.h"
6#include "imagedev/cassette.h"
57
68#define SCREEN_TAG      "screen"
79#define Z80_TAG         "z80"
r11473r11474
2628#define IS_CARTRIDGE_MUSIC_EDITOR(ptr) \
2729   (!strncmp("PIANO", (const char *)&ptr[0x0841], 5))
2830
31extern const TMS9928a_interface tms9928a_interface;
32INPUT_PORTS_EXTERN( sk1100 );
33extern INTERRUPT_GEN( sg1000_int );
34extern const i8255_interface ( sc3000_ppi_intf );
35extern const cassette_config sc3000_cassette_config;
36
2937class sg1000_state : public driver_device
3038{
3139public:
r11473r11474
7381   DECLARE_WRITE8_MEMBER( ppi_pc_w );
7482};
7583
76class sf7000_state : public sc3000_state
77{
78public:
79   sf7000_state(const machine_config &mconfig, device_type type, const char *tag)
80      : sc3000_state(mconfig, type, tag),
81        m_fdc(*this, UPD765_TAG),
82        m_centronics(*this, CENTRONICS_TAG),
83        m_floppy0(*this, FLOPPY_0)
84   { }
85
86   required_device<device_t> m_fdc;
87   required_device<device_t> m_centronics;
88   required_device<device_t> m_floppy0;
89
90   virtual void machine_start();
91   virtual void machine_reset();
92
93   DECLARE_READ8_MEMBER( ppi_pa_r );
94   DECLARE_WRITE8_MEMBER( ppi_pc_w );
95   DECLARE_WRITE_LINE_MEMBER( fdc_intrq_w );
96
97   /* floppy state */
98   int m_fdc_irq;
99   int m_fdc_index;
100};
101
10284#endif
src/mame/drivers/sg1000.c
r11473r11474
6666
6767#include "emu.h"
6868#include "cpu/z80/z80.h"
69#include "imagedev/flopdrv.h"
7069#include "imagedev/cartslot.h"
7170#include "imagedev/cassette.h"
7271#include "machine/ram.h"
7372#include "imagedev/printer.h"
74#include "formats/basicdsk.h"
7573#include "machine/ctronics.h"
7674#include "machine/i8255.h"
77#include "machine/msm8251.h"
78#include "machine/upd765.h"
7975#include "sound/sn76496.h"
80#include "video/tms9928a.h"
8176#include "crsshair.h"
8277#include "includes/sg1000.h"
8378
r11473r11474
237232ADDRESS_MAP_END
238233*/
239234
240/*-------------------------------------------------
241    ADDRESS_MAP( sf7000_map )
242-------------------------------------------------*/
243
244static ADDRESS_MAP_START( sf7000_map, AS_PROGRAM, 8, sf7000_state )
245   AM_RANGE(0x0000, 0x3fff) AM_READ_BANK("bank1") AM_WRITE_BANK("bank2")
246   AM_RANGE(0x4000, 0xffff) AM_RAM
247ADDRESS_MAP_END
248
249/*-------------------------------------------------
250    ADDRESS_MAP( sf7000_io_map )
251-------------------------------------------------*/
252
253static ADDRESS_MAP_START( sf7000_io_map, AS_IO, 8, sf7000_state )
254   ADDRESS_MAP_GLOBAL_MASK(0xff)
255   AM_RANGE(0x7f, 0x7f) AM_DEVWRITE_LEGACY(SN76489A_TAG, sn76496_w)
256   AM_RANGE(0xbe, 0xbe) AM_READWRITE_LEGACY(TMS9928A_vram_r, TMS9928A_vram_w)
257   AM_RANGE(0xbf, 0xbf) AM_READWRITE_LEGACY(TMS9928A_register_r, TMS9928A_register_w)
258   AM_RANGE(0xdc, 0xdf) AM_DEVREADWRITE(UPD9255_0_TAG, i8255_device, read, write)
259   AM_RANGE(0xe0, 0xe0) AM_DEVREAD_LEGACY(UPD765_TAG, upd765_status_r)
260   AM_RANGE(0xe1, 0xe1) AM_DEVREADWRITE_LEGACY(UPD765_TAG, upd765_data_r, upd765_data_w)
261   AM_RANGE(0xe4, 0xe7) AM_DEVREADWRITE(UPD9255_1_TAG, i8255_device, read, write)
262   AM_RANGE(0xe8, 0xe8) AM_DEVREADWRITE_LEGACY(UPD8251_TAG, msm8251_data_r, msm8251_data_w)
263   AM_RANGE(0xe9, 0xe9) AM_DEVREADWRITE_LEGACY(UPD8251_TAG, msm8251_status_r, msm8251_control_w)
264ADDRESS_MAP_END
265
266235/***************************************************************************
267236    INPUT PORTS
268237***************************************************************************/
r11473r11474
378347    INPUT_PORTS( sk1100 )
379348-------------------------------------------------*/
380349
381static INPUT_PORTS_START( sk1100 )
350INPUT_PORTS_START( sk1100 )
382351   PORT_START("PA0")
383352   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_1) PORT_CHAR('1') PORT_CHAR('!')
384353   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_Q) PORT_CHAR('Q') PORT_CHAR('q')
r11473r11474
509478   PORT_INCLUDE( tvdraw )
510479INPUT_PORTS_END
511480
512/*-------------------------------------------------
513    INPUT_PORTS( sf7000 )
514-------------------------------------------------*/
515
516static INPUT_PORTS_START( sf7000 )
517   PORT_INCLUDE( sk1100 )
518
519   PORT_START("BAUD")
520   PORT_CONFNAME( 0x05, 0x05, "Baud rate")
521   PORT_CONFSETTING( 0x00, "9600 baud" )
522   PORT_CONFSETTING( 0x01, "4800 baud" )
523   PORT_CONFSETTING( 0x02, "2400 baud" )
524   PORT_CONFSETTING( 0x03, "1200 baud" )
525   PORT_CONFSETTING( 0x04, "600 baud" )
526   PORT_CONFSETTING( 0x05, "300 baud" )
527INPUT_PORTS_END
528
529481/***************************************************************************
530482    DEVICE CONFIGURATION
531483***************************************************************************/
r11473r11474
534486    TMS9928a_interface tms9928a_interface
535487-------------------------------------------------*/
536488
537static INTERRUPT_GEN( sg1000_int )
489INTERRUPT_GEN( sg1000_int )
538490{
539491    TMS9928A_interrupt(device->machine());
540492}
r11473r11474
544496   cputag_set_input_line(machine, Z80_TAG, INPUT_LINE_IRQ0, state);
545497}
546498
547static const TMS9928a_interface tms9928a_interface =
499const TMS9928a_interface tms9928a_interface =
548500{
549501   TMS99x8A,
550502   0x4000,
r11473r11474
632584   /* TODO printer */
633585}
634586
635static I8255_INTERFACE( sc3000_ppi_intf )
587I8255_INTERFACE( sc3000_ppi_intf )
636588{
637589   DEVCB_DRIVER_MEMBER(sc3000_state, ppi_pa_r),   // Port A read
638590   DEVCB_NULL,                              // Port A write
r11473r11474
646598    cassette_config sc3000_cassette_config
647599-------------------------------------------------*/
648600
649static const cassette_config sc3000_cassette_config =
601const cassette_config sc3000_cassette_config =
650602{
651603   cassette_default_formats,
652604   NULL,
r11473r11474
654606   NULL
655607};
656608
657/*-------------------------------------------------
658    I8255_INTERFACE( sf7000_ppi_intf )
659-------------------------------------------------*/
660
661READ8_MEMBER( sf7000_state::ppi_pa_r )
662{
663   /*
664        Signal  Description
665
666        PA0     INT from FDC
667        PA1     BUSY from Centronics printer
668        PA2     INDEX from FDD
669        PA3
670        PA4
671        PA5
672        PA6
673        PA7
674    */
675
676   UINT8 data = 0;
677
678   data |= m_fdc_irq;
679   data |= centronics_busy_r(m_centronics) << 1;
680   data |= m_fdc_index << 2;
681
682   return data;
683}
684
685WRITE8_MEMBER( sf7000_state::ppi_pc_w )
686{
687   /*
688        Signal  Description
689
690        PC0     /INUSE signal to FDD
691        PC1     /MOTOR ON signal to FDD
692        PC2     TC signal to FDC
693        PC3     RESET signal to FDC
694        PC4     not connected
695        PC5     not connected
696        PC6     /ROM SEL (switch between IPL ROM and RAM)
697        PC7     /STROBE to Centronics printer
698    */
699
700   /* floppy motor */
701   floppy_mon_w(m_floppy0, BIT(data, 1));
702   floppy_drive_set_ready_state(m_floppy0, 1, 1);
703
704   /* FDC terminal count */
705   upd765_tc_w(m_fdc, BIT(data, 2));
706
707   /* FDC reset */
708   if (BIT(data, 3))
709   {
710      upd765_reset(m_fdc, 0);
711   }
712
713   /* ROM selection */
714   memory_set_bank(machine(), "bank1", BIT(data, 6));
715
716   /* printer strobe */
717   centronics_strobe_w(m_centronics, BIT(data, 7));
718}
719
720static I8255_INTERFACE( sf7000_ppi_intf )
721{
722   DEVCB_DRIVER_MEMBER(sf7000_state, ppi_pa_r),            // Port A read
723   DEVCB_NULL,                                       // Port A write
724   DEVCB_NULL,                                       // Port B read
725   DEVCB_DEVICE_HANDLER(CENTRONICS_TAG, centronics_data_w),   // Port B write
726   DEVCB_NULL,                                       // Port C read
727   DEVCB_DRIVER_MEMBER(sf7000_state, ppi_pc_w)               // Port C write
728};
729
730/*-------------------------------------------------
731    upd765_interface sf7000_upd765_interface
732-------------------------------------------------*/
733
734WRITE_LINE_MEMBER( sf7000_state::fdc_intrq_w )
735{
736   m_fdc_irq = state;
737}
738
739static const struct upd765_interface sf7000_upd765_interface =
740{
741   DEVCB_DRIVER_LINE_MEMBER(sf7000_state, fdc_intrq_w),
742   DEVCB_NULL,
743   NULL,
744   UPD765_RDY_PIN_CONNECTED,
745   { FLOPPY_0, NULL, NULL, NULL }
746};
747
748/*-------------------------------------------------
749    FLOPPY_OPTIONS( sf7000 )
750-------------------------------------------------*/
751
752static FLOPPY_OPTIONS_START( sf7000 )
753   FLOPPY_OPTION(sf7000, "sf7", "SF7 disk image", basicdsk_identify_default, basicdsk_construct_default, NULL,
754      HEADS([1])
755      TRACKS([40])
756      SECTORS([16])
757      SECTOR_LENGTH([256])
758      FIRST_SECTOR_ID([1]))
759FLOPPY_OPTIONS_END
760
761/*-------------------------------------------------
762    floppy_config sf7000_floppy_config
763-------------------------------------------------*/
764
765static const floppy_config sf7000_floppy_config =
766{
767   DEVCB_NULL,
768   DEVCB_NULL,
769   DEVCB_NULL,
770   DEVCB_NULL,
771   DEVCB_NULL,
772   FLOPPY_STANDARD_5_25_DSHD,
773   FLOPPY_OPTIONS_NAME(sf7000),
774   NULL
775};
776
777609/***************************************************************************
778610    CARTRIDGE LOADING
779611***************************************************************************/
r11473r11474
994826   state_save_register_global(machine(), m_keylatch);
995827}
996828
997/*-------------------------------------------------
998    sf7000_fdc_index_callback -
999-------------------------------------------------*/
1000
1001static void sf7000_fdc_index_callback(device_t *controller, device_t *img, int state)
1002{
1003   sf7000_state *driver_state = img->machine().driver_data<sf7000_state>();
1004
1005   driver_state->m_fdc_index = state;
1006}
1007
1008/*-------------------------------------------------
1009    MACHINE_START( sf7000 )
1010-------------------------------------------------*/
1011
1012void sf7000_state::machine_start()
1013{
1014   /* configure VDP */
1015   TMS9928A_configure(&tms9928a_interface);
1016
1017   /* configure FDC */
1018   floppy_drive_set_index_pulse_callback(m_floppy0, sf7000_fdc_index_callback);
1019
1020   /* configure memory banking */
1021   memory_configure_bank(machine(), "bank1", 0, 1, machine().region(Z80_TAG)->base(), 0);
1022   memory_configure_bank(machine(), "bank1", 1, 1, ram_get_ptr(m_ram), 0);
1023   memory_configure_bank(machine(), "bank2", 0, 1, ram_get_ptr(m_ram), 0);
1024
1025   /* register for state saving */
1026   state_save_register_global(machine(), m_keylatch);
1027   state_save_register_global(machine(), m_fdc_irq);
1028   state_save_register_global(machine(), m_fdc_index);
1029}
1030
1031/*-------------------------------------------------
1032    MACHINE_RESET( sf7000 )
1033-------------------------------------------------*/
1034
1035void sf7000_state::machine_reset()
1036{
1037   memory_set_bank(machine(), "bank1", 0);
1038   memory_set_bank(machine(), "bank2", 0);
1039}
1040
1041829/***************************************************************************
1042830    MACHINE DRIVERS
1043831***************************************************************************/
r11473r11474
1138926   MCFG_RAM_DEFAULT_SIZE("2K")
1139927MACHINE_CONFIG_END
1140928
1141/*-------------------------------------------------
1142    MACHINE_CONFIG_START( sf7000, sf7000_state )
1143-------------------------------------------------*/
1144929
1145static MACHINE_CONFIG_START( sf7000, sf7000_state )
1146   /* basic machine hardware */
1147   MCFG_CPU_ADD(Z80_TAG, Z80, XTAL_10_738635MHz/3)
1148   MCFG_CPU_PROGRAM_MAP(sf7000_map)
1149   MCFG_CPU_IO_MAP(sf7000_io_map)
1150   MCFG_CPU_VBLANK_INT(SCREEN_TAG, sg1000_int)
1151
1152    /* video hardware */
1153   MCFG_FRAGMENT_ADD(tms9928a)
1154   MCFG_SCREEN_MODIFY(SCREEN_TAG)
1155   MCFG_SCREEN_REFRESH_RATE((float)XTAL_10_738635MHz/2/342/262)
1156   MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(2500)) /* not accurate */
1157
1158   /* sound hardware */
1159   MCFG_SPEAKER_STANDARD_MONO("mono")
1160   MCFG_SOUND_ADD(SN76489A_TAG, SN76489A, XTAL_10_738635MHz/3)
1161   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
1162
1163   /* devices */
1164   MCFG_I8255_ADD(UPD9255_0_TAG, sc3000_ppi_intf)
1165   MCFG_I8255_ADD(UPD9255_1_TAG, sf7000_ppi_intf)
1166   MCFG_MSM8251_ADD(UPD8251_TAG, default_msm8251_interface)
1167   MCFG_UPD765A_ADD(UPD765_TAG, sf7000_upd765_interface)
1168   MCFG_FLOPPY_DRIVE_ADD(FLOPPY_0, sf7000_floppy_config)
1169//  MCFG_PRINTER_ADD("sp400") /* serial printer */
1170   MCFG_CENTRONICS_ADD(CENTRONICS_TAG, standard_centronics)
1171   MCFG_CASSETTE_ADD(CASSETTE_TAG, sc3000_cassette_config)
1172
1173   /* internal ram */
1174   MCFG_RAM_ADD(RAM_TAG)
1175   MCFG_RAM_DEFAULT_SIZE("64K")
1176MACHINE_CONFIG_END
1177
1178930/***************************************************************************
1179931    ROMS
1180932***************************************************************************/
r11473r11474
1202954   ROM_LOAD( "omvbios.bin", 0x0000, 0x4000, CRC(c5a67b95) SHA1(6d7c64dd60dee4a33061d3d3a7c2ed190d895cdb) )
1203955ROM_END
1204956
1205ROM_START( sf7000 )
1206   ROM_REGION( 0x10000, Z80_TAG, 0 )
1207   ROM_LOAD( "ipl.rom", 0x0000, 0x2000, CRC(d76810b8) SHA1(77339a6db2593aadc638bed77b8e9bed5d9d87e3) )
1208ROM_END
1209
1210957/***************************************************************************
1211958    SYSTEM DRIVERS
1212959***************************************************************************/
r11473r11474
1216963CONS( 1984,   sg1000m2,   sg1000,     0,          sc3000,     sc3000,     0,      "Sega",             "SG-1000 II",                               GAME_SUPPORTS_SAVE )
1217964COMP( 1983,   sc3000,     0,          sg1000,     sc3000,     sc3000,     0,      "Sega",             "SC-3000",                                  GAME_SUPPORTS_SAVE )
1218965COMP( 1983,   sc3000h,    sc3000,     0,          sc3000,     sc3000,     0,      "Sega",             "SC-3000H",                                 GAME_SUPPORTS_SAVE )
1219COMP( 1983,   sf7000,     sc3000,     0,          sf7000,     sf7000,     0,      "Sega",             "SC-3000/Super Control Station SF-7000",    GAME_SUPPORTS_SAVE )
1220966CONS( 1984,   omv1000,    sg1000,     0,          omv,        omv,        0,      "Tsukuda Original", "Othello Multivision FG-1000",              GAME_SUPPORTS_SAVE )
1221967CONS( 1984,   omv2000,    sg1000,     0,          omv,        omv,        0,      "Tsukuda Original", "Othello Multivision FG-2000",              GAME_SUPPORTS_SAVE )
src/emu/emu.mak
r11473r11474
207207   $(EMUMACHINE)/msm5832.o   \
208208   $(EMUMACHINE)/msm58321.o   \
209209   $(EMUMACHINE)/msm6242.o      \
210   $(EMUMACHINE)/msm8251.o      \
211210   $(EMUMACHINE)/nmc9306.o      \
212211   $(EMUMACHINE)/nvram.o      \
213212   $(EMUMACHINE)/pc16552d.o   \
r11473r11474
229228   $(EMUMACHINE)/scsidev.o      \
230229   $(EMUMACHINE)/scsihd.o      \
231230   $(EMUMACHINE)/secflash.o   \
232   $(EMUMACHINE)/serial.o   \
233231   $(EMUMACHINE)/smc91c9x.o   \
234232   $(EMUMACHINE)/timekpr.o      \
235233   $(EMUMACHINE)/tmp68301.o   \
r11473r11474
237235   $(EMUMACHINE)/upd1990a.o   \
238236   $(EMUMACHINE)/upd4701.o      \
239237   $(EMUMACHINE)/upd7201.o      \
240   $(EMUMACHINE)/upd765.o      \
241238   $(EMUMACHINE)/wd17xx.o      \
242239   $(EMUMACHINE)/wd33c93.o      \
243240   $(EMUMACHINE)/x2212.o      \
src/emu/machine/upd765.c
r11473r11474
1/***************************************************************************
2
3    machine/upd765.c
4
5    Functions to emulate a UPD765/Intel 8272 compatible floppy disk controller
6
7    Code by Kevin Thacker.
8
9    TODO:
10
11    - overrun condition
12    - Scan Commands
13    - crc error in id field and crc error in data field errors
14    - disc not present, and no sectors on track for data, deleted data, write, write deleted,
15        read a track etc
16        - end of cylinder condition - almost working, needs fixing  with
17                PCW and PC drivers
18    - resolve "ready" state stuff (ready state when reset for PC, ready state change while processing command AND
19    while idle)
20
21    Changes:
22    091006 (Mariusz Wojcieszek, changes needed by QX-10):
23    - allowed "Sense Interrupt Status" command when Seek is active
24    - DIO bit in status register (0x40) is cleared when "Read Data" command is executed,
25      it is later set during result phase.
26
27***************************************************************************/
28
29#include "emu.h"
30#include "machine/upd765.h"
31
32
33typedef enum
34{
35   UPD765_COMMAND_PHASE_FIRST_BYTE,
36   UPD765_COMMAND_PHASE_BYTES,
37   UPD765_RESULT_PHASE,
38   UPD765_EXECUTION_PHASE_READ,
39   UPD765_EXECUTION_PHASE_WRITE
40} UPD765_PHASE;
41
42/* supported versions */
43typedef enum
44{
45   TYPE_UPD765A = 0,
46   TYPE_UPD765B = 1,
47   TYPE_SMC37C78 = 2,
48   TYPE_UPD72065 = 3
49} UPD765_VERSION;
50
51
52/* uncomment the following line for verbose information */
53#define LOG_VERBOSE      0
54#define LOG_COMMAND      0
55#define LOG_EXTRA      0
56#define LOG_INTERRUPT   0
57
58/* uncomment this to not allow end of cylinder "error" */
59#define NO_END_OF_CYLINDER
60
61
62
63/* state of upd765 Interrupt (INT) output */
64#define UPD765_INT   0x02
65/* data rate for floppy discs (MFM data) */
66#define UPD765_DATA_RATE   32
67/* state of upd765 terminal count input*/
68#define UPD765_TC   0x04
69
70#define UPD765_DMA_MODE 0x08
71
72#define UPD765_SEEK_OPERATION_IS_RECALIBRATE 0x01
73
74#define UPD765_SEEK_ACTIVE 0x010
75/* state of upd765 DMA DRQ output */
76#define UPD765_DMA_DRQ 0x020
77/* state of upd765 FDD READY input */
78#define UPD765_FDD_READY 0x040
79
80#define UPD765_MF   0x40
81
82#define UPD765_RESET 0x080
83
84typedef struct _upd765_t upd765_t;
85struct _upd765_t
86{
87   devcb_resolved_write_line   out_int_func;
88   devcb_resolved_write_line   out_drq_func;
89
90   unsigned long   sector_counter;
91   /* version of fdc to emulate */
92   UPD765_VERSION version;
93
94   /* main status register */
95   unsigned char    FDC_main;
96   /* data register */
97   unsigned char   upd765_data_reg;
98
99   unsigned char c,h,r,n;
100
101   int sector_id;
102
103   int data_type;
104
105   char format_data[4];
106
107   UPD765_PHASE    upd765_phase;
108   unsigned int    upd765_command_bytes[16];
109   unsigned int    upd765_result_bytes[16];
110   unsigned int    upd765_transfer_bytes_remaining;
111   unsigned int    upd765_transfer_bytes_count;
112   unsigned int    upd765_status[4];
113   /* present cylinder number per drive */
114   unsigned int    pcn[4];
115
116   /* drive being accessed. drive outputs from fdc */
117   unsigned int    drive;
118   /* side being accessed: side output from fdc */
119   unsigned int   side;
120
121
122   /* step rate time in us */
123   unsigned long   srt_in_ms;
124
125   unsigned int   ncn;
126
127//  unsigned int    upd765_id_index;
128   char *execution_phase_data;
129   unsigned int   upd765_flags;
130
131//  unsigned char specify[2];
132//  unsigned char perpendicular_mode[1];
133
134   int command;
135
136   emu_timer *seek_timer;
137   emu_timer *timer;
138   int timer_type;
139
140   emu_timer *command_timer;
141
142   char *data_buffer;
143   const upd765_interface *intf;
144};
145
146//static void upd765_setup_data_request(unsigned char Data);
147static void upd765_setup_command(device_t *device);
148static TIMER_CALLBACK(upd765_continue_command);
149static int upd765_sector_count_complete(device_t *device);
150static void upd765_increment_sector(device_t *device);
151static void upd765_update_state(device_t *device);
152static void upd765_set_dma_drq(device_t *device,int state);
153static void upd765_set_int(device_t *device,int state);
154
155static const INT8 upd765_cmd_size[32] =
156{
157   1,1,9,3,2,9,9,2,1,9,2,1,9,6,1,3,
158   1,9,1,1,1,1,9,1,1,9,1,1,1,9,1,1
159};
160
161INLINE upd765_t *get_safe_token(device_t *device)
162{
163   assert(device != NULL);
164   assert(device->type() == UPD765A || device->type() == UPD765B ||
165      device->type() == SMC37C78 || device->type() == UPD72065);
166
167   return (upd765_t *)downcast<legacy_device_base *>(device)->token();
168}
169
170static device_t *current_image(device_t *device)
171{
172   device_t *image = NULL;
173   upd765_t *fdc = get_safe_token(device);
174
175   if (!fdc->intf->get_image)
176   {
177      if (fdc->intf->floppy_drive_tags[fdc->drive] != NULL)
178      {
179         if (device->owner() != NULL)
180            image = device->owner()->subdevice(fdc->intf->floppy_drive_tags[fdc->drive]);
181         else
182            image = device->machine().device(fdc->intf->floppy_drive_tags[fdc->drive]);
183      }
184   }
185   else
186   {
187      image = fdc->intf->get_image(device, fdc->drive);
188   }
189   return image;
190}
191
192static void upd765_setup_drive_and_side(device_t *device)
193{
194   upd765_t *fdc = get_safe_token(device);
195   /* drive index upd765 sees */
196   fdc->drive = fdc->upd765_command_bytes[1] & 0x03;
197   /* side index upd765 sees */
198   fdc->side = (fdc->upd765_command_bytes[1]>>2) & 0x01;
199}
200
201
202/* setup status register 0 based on data in status register 1 and 2 */
203static void upd765_setup_st0(device_t *device)
204{
205   upd765_t *fdc = get_safe_token(device);
206   /* clear completition status bits, drive bits and side bits */
207   fdc->upd765_status[0] &= ~((1<<7) | (1<<6) | (1<<2) | (1<<1) | (1<<0));
208   /* fill in drive */
209   fdc->upd765_status[0] |= fdc->drive | (fdc->side<<2);
210
211   /* fill in completion status bits based on bits in st0, st1, st2 */
212   /* no error bits set */
213   if ((fdc->upd765_status[1] | fdc->upd765_status[2])==0)
214   {
215      return;
216   }
217
218   fdc->upd765_status[0] |= 0x040;
219}
220
221
222static int upd765_n_to_bytes(int n)
223{
224   /* 0-> 128 bytes, 1->256 bytes, 2->512 bytes etc */
225   /* data_size = ((1<<(N+7)) */
226   return 1<<(n+7);
227}
228
229static void upd765_set_data_request(device_t *device)
230{
231   upd765_t *fdc = get_safe_token(device);
232   fdc->FDC_main |= 0x080;
233}
234
235static void upd765_clear_data_request(device_t *device)
236{
237   upd765_t *fdc = get_safe_token(device);
238   fdc->FDC_main &= ~0x080;
239}
240
241static int upd765_get_rdy(device_t *device)
242{
243   upd765_t *fdc = get_safe_token(device);
244
245   if (fdc->intf->rdy_pin == UPD765_RDY_PIN_CONNECTED)
246   {
247      device_t *img = current_image(device);
248      return floppy_drive_get_flag_state(img, FLOPPY_DRIVE_READY);
249   }
250   else
251      return 1;
252}
253
254static void upd765_seek_complete(device_t *device)
255{
256   /* tested on Amstrad CPC */
257
258   /* if a seek is done without drive connected: */
259   /*  abnormal termination of command,
260        seek complete,
261        not ready
262    */
263
264   /* if a seek is done with drive connected, but disc missing: */
265   /* abnormal termination of command,
266        seek complete,
267        not ready */
268
269   /* if a seek is done with drive connected and disc in drive */
270   /* seek complete */
271
272
273   /* On the PC however, it appears that recalibrates and seeks can be performed without
274    a disc in the drive. */
275
276   /* Therefore, the above output is dependant on the state of the drive */
277
278   /* In the Amstrad CPC, the drive select is provided by the UPD765. A single port is also
279    assigned for setting the drive motor state. The motor state controls the motor of the selected
280    drive */
281
282   /* On the PC the drive can be selected with the DIGITAL OUTPUT REGISTER, and the motor of each
283    of the 4 possible drives is also settable using the same register */
284
285   /* Assumption for PC: (NOT TESTED - NEEDS VERIFICATION) */
286
287   /* If a seek is done without drive connected: */
288   /* abnormal termination of command,
289        seek complete,
290        fault
291        */
292
293   /* if a seek is done with drive connected, but disc missing: */
294   /* seek complete */
295
296   /* if a seek is done with drive connected and disc in drive: */
297   /* seek complete */
298
299   /* On Amstrad CPC:
300        If drive not connected, or drive connected but disc not in drive, not ready!
301        If drive connected and drive motor on, ready!
302       On PC:
303        Drive is always ready!
304
305        In 37c78 docs, the ready bits of the upd765 are marked as unused.
306        This indicates it is always ready!!!!!
307    */
308
309   device_t *img = current_image(device);
310   upd765_t *fdc = get_safe_token(device);
311
312   fdc->pcn[fdc->drive] = fdc->ncn;
313
314   fdc->upd765_status[0] = 0x20;
315
316   /* drive ready? */
317   if (img != NULL && upd765_get_rdy(device))
318   {
319      /* recalibrate? */
320      if (fdc->upd765_flags & UPD765_SEEK_OPERATION_IS_RECALIBRATE)
321      {
322         /* not at track 0? */
323         if (fdc->pcn[fdc->drive] != 0)
324            /* no, track 0 failed after 77 steps */
325            fdc->upd765_status[0] |= 0x40 | 0x10;
326      }
327   }
328   else
329   {
330      /* abnormal termination, not ready */
331      fdc->upd765_status[0] |= 0x40 | 0x08;
332   }
333
334   /* set drive and side. note: commented out side to avoid problems with the tf20 */
335   fdc->upd765_status[0] |= fdc->drive; //| (fdc->side<<2);
336
337   upd765_set_int(device,0);
338   upd765_set_int(device,1);
339
340   fdc->upd765_flags &= ~UPD765_SEEK_ACTIVE;
341
342   upd765_idle(device);
343}
344
345static TIMER_CALLBACK(upd765_seek_timer_callback)
346{
347   device_t *device = (device_t *)ptr;
348   upd765_t *fdc = get_safe_token(device);
349   /* seek complete */
350   upd765_seek_complete(device);
351
352   fdc->seek_timer->reset();
353}
354
355static void upd765_timer_func(device_t *device, int timer_type)
356{
357   upd765_t *fdc = get_safe_token(device);
358   /* type 0 = data transfer mode in execution phase */
359   if (fdc->timer_type == 0)
360   {
361      /* set data request */
362      upd765_set_data_request(device);
363
364      fdc->timer_type = 4;
365
366      if (!(fdc->upd765_flags & UPD765_DMA_MODE))
367      {
368         if (fdc->upd765_command_bytes[0] & UPD765_MF)
369         {
370            /* MFM */
371            fdc->timer->reset(attotime::from_usec(13));
372         }
373         else
374         {
375            /* FM */
376            fdc->timer->reset(attotime::from_usec(27));
377         }
378      }
379      else
380      {
381         upd765_timer_func(device, fdc->timer_type);
382      }
383   }
384   else if (fdc->timer_type==2)
385   {
386      /* result phase begin */
387
388      /* generate a int for specific commands */
389      switch (fdc->command) {
390      case 2:      /* read a track */
391      case 5:      /* write data */
392      case 6:      /* read data */
393      case 9:      /* write deleted data */
394      case 10:   /* read id */
395      case 12:   /* read deleted data */
396      case 13:   /* format at track */
397      case 17:   /* scan equal */
398      case 19:   /* scan low or equal */
399      case 29:   /* scan high or equal */
400         upd765_set_int(device,1);
401         break;
402
403      default:
404         break;
405      }
406
407      upd765_set_data_request(device);
408
409      fdc->timer->reset();
410   }
411   else if (fdc->timer_type == 4)
412   {
413      /* if in dma mode, a int is not generated per byte. If not in  DMA mode
414        a int is generated per byte */
415      if (fdc->upd765_flags & UPD765_DMA_MODE)
416      {
417         upd765_set_dma_drq(device,1);
418      }
419      else
420      {
421         if (fdc->FDC_main & (1<<7))
422         {
423            /* set int to indicate data is ready */
424            upd765_set_int(device,1);
425         }
426      }
427
428      fdc->timer->reset();
429   }
430}
431
432static TIMER_CALLBACK(upd765_timer_callback)
433{
434   device_t *device = (device_t *)ptr;
435   upd765_timer_func(device,param);
436}
437
438/* after (32-27) the DRQ is set, then 27 us later, the int is set.
439I don't know if this is correct, but it is required for the PCW driver.
440In this driver, the first NMI calls the handler function, furthur NMI's are
441effectively disabled by reading the data before the NMI int can be set.
442*/
443
444static void upd765_setup_timed_generic(device_t *device, int timer_type, attotime duration)
445{
446   upd765_t *fdc = get_safe_token(device);
447
448   fdc->timer_type = timer_type;
449
450   if (!(fdc->upd765_flags & UPD765_DMA_MODE))
451   {
452      fdc->timer->adjust(duration);
453   }
454   else
455   {
456      upd765_timer_func(device,fdc->timer_type);
457      fdc->timer->reset();
458   }
459}
460
461/* setup data request */
462static void upd765_setup_timed_data_request(device_t *device, int bytes)
463{
464   /* setup timer to trigger in UPD765_DATA_RATE us */
465   upd765_setup_timed_generic(device, 0, attotime::from_usec(32-27)   /*UPD765_DATA_RATE)*bytes*/);
466}
467
468/* setup result data request */
469static void upd765_setup_timed_result_data_request(device_t *device)
470{
471   upd765_setup_timed_generic(device, 2, attotime::from_usec(UPD765_DATA_RATE*2));
472}
473
474
475/* sets up a timer to issue a seek complete in signed_tracks time */
476static void upd765_setup_timed_int(device_t *device,int signed_tracks)
477{
478   upd765_t *fdc = get_safe_token(device);
479   /* setup timer to signal after seek time is complete */
480   fdc->seek_timer->adjust(attotime::from_double(fdc->srt_in_ms*abs(signed_tracks)*0.001));
481}
482
483static void upd765_seek_setup(device_t *device, int is_recalibrate)
484{
485   device_t *img;
486   int signed_tracks;
487   upd765_t *fdc = get_safe_token(device);
488
489   fdc->upd765_flags |= UPD765_SEEK_ACTIVE;
490
491   if (is_recalibrate)
492   {
493      /* head cannot be specified with recalibrate */
494      fdc->upd765_command_bytes[1] &=~0x04;
495   }
496
497   upd765_setup_drive_and_side(device);
498
499   img = current_image(device);
500
501   fdc->FDC_main |= (1<<fdc->drive);
502   fdc->FDC_main |= 0x20;  // execution phase
503   fdc->FDC_main &= ~0x10;  // not busy, can send another seek/recalibrate
504                            // for a different drive, or sense int status
505
506   /* recalibrate command? */
507   if (is_recalibrate)
508   {
509      fdc->upd765_flags |= UPD765_SEEK_OPERATION_IS_RECALIBRATE;
510
511      fdc->ncn = 0;
512
513      /* if drive is already at track 0, or drive is not ready */
514      if (img == NULL || floppy_tk00_r(img) == CLEAR_LINE || (!upd765_get_rdy(device)))
515      {
516         /* seek completed */
517//          upd765_seek_complete(device);
518         // delay for the time of 1 step, the PCW does not like immediate recalibrates
519         upd765_setup_timed_int(device,1);
520      }
521      else
522      {
523         /* is drive present? */
524         if (1) //image_slotexists(img)) //fix me
525         {
526            /* yes - calculate real number of tracks to seek */
527
528            int current_track;
529
530            /* get current track */
531            current_track = floppy_drive_get_current_track(img);
532
533            /* get number of tracks to seek */
534            signed_tracks = -current_track;
535         }
536         else
537         {
538            /* no, seek 77 tracks and then stop */
539            /* true for UPD765A, but not for other variants */
540            signed_tracks = -77;
541         }
542
543         if (signed_tracks!=0)
544         {
545            /* perform seek - if drive isn't present it will not do anything */
546            floppy_drive_seek(img, signed_tracks);
547
548            upd765_setup_timed_int(device,signed_tracks);
549         }
550         else
551         {
552            upd765_seek_complete(device);
553         }
554      }
555   }
556   else
557   {
558
559      fdc->upd765_flags &= ~UPD765_SEEK_OPERATION_IS_RECALIBRATE;
560
561      fdc->ncn = fdc->upd765_command_bytes[2];
562
563      /* get signed tracks */
564      signed_tracks = fdc->ncn - fdc->pcn[fdc->drive];
565
566      /* if no tracks to seek, or drive is not ready, seek is complete */
567      if (img == NULL || (signed_tracks==0) || (!upd765_get_rdy(device)))
568      {
569         upd765_seek_complete(device);
570      }
571      else
572      {
573         /* perform seek - if drive isn't present it will not do anything */
574         floppy_drive_seek(img, signed_tracks);
575
576         /* seek complete - issue an interrupt */
577         upd765_setup_timed_int(device,signed_tracks);
578      }
579   }
580//    upd765_idle(device);
581}
582
583
584
585static void upd765_setup_execution_phase_read(device_t *device, char *ptr, int size)
586{
587   upd765_t *fdc = get_safe_token(device);
588
589   fdc->FDC_main &= ~0x040;                     /* FDC->CPU */
590
591   fdc->upd765_transfer_bytes_count = 0;
592   fdc->upd765_transfer_bytes_remaining = size;
593   fdc->execution_phase_data = ptr;
594   fdc->upd765_phase = UPD765_EXECUTION_PHASE_READ;
595
596   upd765_setup_timed_data_request(device, 1);
597}
598
599static void upd765_setup_execution_phase_write(device_t *device, char *ptr, int size)
600{
601   upd765_t *fdc = get_safe_token(device);
602
603   fdc->FDC_main &= ~0x040;                     /* FDC->CPU */
604
605   fdc->upd765_transfer_bytes_count = 0;
606   fdc->upd765_transfer_bytes_remaining = size;
607   fdc->execution_phase_data = ptr;
608   fdc->upd765_phase = UPD765_EXECUTION_PHASE_WRITE;
609
610   /* setup a data request with first byte */
611   upd765_setup_timed_data_request(device,1);
612}
613
614
615static void upd765_setup_result_phase(device_t *device, int byte_count)
616{
617   upd765_t *fdc = get_safe_token(device);
618
619   fdc->FDC_main |= 0x040;                     /* FDC->CPU */
620   fdc->FDC_main &= ~0x020;                    /* not execution phase */
621
622   fdc->upd765_transfer_bytes_count = 0;
623   fdc->upd765_transfer_bytes_remaining = byte_count;
624   fdc->upd765_phase = UPD765_RESULT_PHASE;
625
626   upd765_setup_timed_result_data_request(device);
627}
628
629void upd765_idle(device_t *device)
630{
631   upd765_t *fdc = get_safe_token(device);
632
633   fdc->FDC_main &= ~0x040;                     /* CPU->FDC */
634   fdc->FDC_main &= ~0x020;                    /* not execution phase */
635   fdc->FDC_main &= ~0x010;                     /* not busy */
636   fdc->upd765_phase = UPD765_COMMAND_PHASE_FIRST_BYTE;
637
638   upd765_set_data_request(device);
639}
640
641
642
643/* change flags */
644static void upd765_change_flags(device_t *device,unsigned int flags, unsigned int mask)
645{
646   unsigned int new_flags;
647   unsigned int changed_flags;
648   upd765_t *fdc = get_safe_token(device);
649
650   assert((flags & ~mask) == 0);
651
652   /* compute the new flags and which ones have changed */
653   new_flags = fdc->upd765_flags & ~mask;
654   new_flags |= flags;
655   changed_flags = fdc->upd765_flags ^ new_flags;
656   fdc->upd765_flags = new_flags;
657
658   /* if interrupt changed, call the handler */
659   if (changed_flags & UPD765_INT)
660      fdc->out_int_func((fdc->upd765_flags & UPD765_INT) ? 1 : 0);
661
662   /* if DRQ changed, call the handler */
663   if (changed_flags & UPD765_DMA_DRQ)
664      fdc->out_drq_func((fdc->upd765_flags & UPD765_DMA_DRQ) ? 1 : 0);
665}
666
667
668
669/* set int output */
670static void upd765_set_int(device_t *device, int state)
671{
672   if (LOG_INTERRUPT)
673      logerror("upd765_set_int(): state=%d\n", state);
674   upd765_change_flags(device, state ? UPD765_INT : 0, UPD765_INT);
675}
676
677
678
679/* set dma request output */
680static void upd765_set_dma_drq(device_t *device, int state)
681{
682   upd765_change_flags(device, state ? UPD765_DMA_DRQ : 0, UPD765_DMA_DRQ);
683}
684
685
686
687/* Drive ready */
688
689/*
690
691A drive will report ready if:
692- drive is selected
693- disc is in the drive
694- disk is rotating at a constant speed (normally 300rpm)
695
696On more modern PCs, a ready signal is not provided by the drive.
697This signal is not used in the PC design and was eliminated to save costs
698If you look at the datasheets for the modern UPD765 variants, you will see the Ready
699signal is not mentioned.
700
701On the original UPD765A, ready signal is required, and some commands will fail if the drive
702is not ready.
703
704
705
706
707*/
708
709
710
711
712/* done when ready state of drive changes */
713/* this ignores if command is active, in which case command should terminate immediatly
714with error */
715static void upd765_set_ready_change_callback(device_t *controller, device_t *img, int state)
716{
717   upd765_t *fdc = get_safe_token(controller);
718   int drive = floppy_get_drive(img);
719
720   if (LOG_EXTRA)
721      logerror("upd765: ready state change\n");
722
723   /* drive that changed state */
724   fdc->upd765_status[0] = 0x0c0 | drive;
725
726   /* not ready */
727   if (state==0 && fdc->intf->rdy_pin == UPD765_RDY_PIN_CONNECTED )
728      fdc->upd765_status[0] |= 8;
729
730   /* trigger an int */
731   upd765_set_int(controller, 1);
732}
733
734
735/* terminal count input */
736WRITE_LINE_DEVICE_HANDLER( upd765_tc_w )
737{
738   int old_state;
739   upd765_t *fdc = get_safe_token(device);
740
741   old_state = fdc->upd765_flags;
742
743   /* clear drq */
744   upd765_set_dma_drq(device, 0);
745
746   fdc->upd765_flags &= ~UPD765_TC;
747   if (state)
748   {
749      fdc->upd765_flags |= UPD765_TC;
750   }
751
752   /* changed state? */
753   if (((fdc->upd765_flags^old_state) & UPD765_TC)!=0)
754   {
755      /* now set? */
756      if ((fdc->upd765_flags & UPD765_TC)!=0)
757      {
758         /* yes */
759         if (fdc->timer)
760         {
761            if (fdc->timer_type==0)
762            {
763               fdc->timer->reset();
764
765
766            }
767         }
768
769#ifdef NO_END_OF_CYLINDER
770         fdc->command_timer->adjust(attotime::zero);
771#else
772         upd765_update_state(device);
773#endif
774      }
775   }
776}
777
778READ8_DEVICE_HANDLER( upd765_status_r )
779{
780   upd765_t *fdc = get_safe_token(device);
781   if (LOG_EXTRA)
782      logerror("%s: upd765_status_r: %02x\n", device->machine().describe_context(), fdc->FDC_main);
783   return fdc->FDC_main;
784}
785
786
787/* control mark handling code */
788
789/* if SK==1, and we are executing a read data command, and a deleted data mark is found,
790skip it.
791if SK==1, and we are executing a read deleted data command, and a data mark is found,
792skip it. */
793
794static int upd765_read_skip_sector(device_t *device)
795{
796   upd765_t *fdc = get_safe_token(device);
797   /* skip set? */
798   if ((fdc->upd765_command_bytes[0] & (1<<5))!=0)
799   {
800      /* read data? */
801      if (fdc->command == 0x06)
802      {
803         /* did we just find a sector with deleted data mark? */
804         if (fdc->data_type == UPD765_DAM_DELETED_DATA)
805         {
806            /* skip it */
807            return TRUE;
808         }
809      }
810      /* deleted data? */
811      else
812      if (fdc->command == 0x0c)
813      {
814         /* did we just find a sector with data mark ? */
815         if (fdc->data_type == UPD765_DAM_DATA)
816         {
817            /* skip it */
818            return TRUE;
819         }
820      }
821   }
822
823   /* do not skip */
824   return FALSE;
825}
826
827/* this is much closer to how the upd765 actually gets sectors */
828/* used by read data, read deleted data, write data, write deleted data */
829/* What the upd765 does:
830
831  - get next sector id from disc
832  - if sector id matches id specified in command, it will
833    search for next data block and read data from it.
834
835  - if the index is seen twice while it is searching for a sector, then the sector cannot be found
836*/
837
838static void upd765_get_next_id(device_t *device, chrn_id *id)
839{
840   upd765_t *fdc = get_safe_token(device);
841   device_t *img = current_image(device);
842
843   /* get next id from disc */
844   floppy_drive_get_next_id(img, fdc->side,id);
845
846   fdc->sector_id = id->data_id;
847
848   /* set correct data type */
849   fdc->data_type = UPD765_DAM_DATA;
850   if (id->flags & ID_FLAG_DELETED_DATA)
851   {
852      fdc->data_type = UPD765_DAM_DELETED_DATA;
853   }
854}
855
856static int upd765_get_matching_sector(device_t *device)
857{
858   upd765_t *fdc = get_safe_token(device);
859   device_t *img = current_image(device);
860   chrn_id id;
861
862   /* number of times we have seen index hole */
863   int index_count = 0;
864
865   /* get sector id's */
866   do
867    {
868      upd765_get_next_id(device, &id);
869
870      /* tested on Amstrad CPC - All bytes must match, otherwise
871        a NO DATA error is reported */
872      if (id.R == fdc->upd765_command_bytes[4])
873      {
874         if (id.C == fdc->upd765_command_bytes[2])
875         {
876            if (id.H == fdc->upd765_command_bytes[3])
877            {
878               if (id.N == fdc->upd765_command_bytes[5])
879               {
880                  /* end of cylinder is set if:
881                        1. sector data is read completely (i.e. no other errors occur like
882                        no data.
883                        2. sector being read is same specified by EOT
884                        3. terminal count is not received */
885                  if (fdc->upd765_command_bytes[4]==fdc->upd765_command_bytes[6])
886                  {
887                     /* set end of cylinder */
888                     fdc->upd765_status[1] |= UPD765_ST1_END_OF_CYLINDER;
889                  }
890
891                  return TRUE;
892               }
893            }
894         }
895         else
896         {
897            /* the specified sector ID was found, however, the C value specified
898                in the read/write command did not match the C value read from the disc */
899
900            /* no data - checked on Amstrad CPC */
901            fdc->upd765_status[1] |= UPD765_ST1_NO_DATA;
902            /* bad C value */
903            fdc->upd765_status[2] |= UPD765_ST2_WRONG_CYLINDER;
904
905            if (id.C == 0x0ff)
906            {
907               /* the C value is 0x0ff which indicates a bad track in the IBM soft-sectored
908                    format */
909               fdc->upd765_status[2] |= UPD765_ST2_BAD_CYLINDER;
910            }
911
912            return FALSE;
913         }
914      }
915
916       /* index set? */
917      if (floppy_drive_get_flag_state(img, FLOPPY_DRIVE_INDEX))
918      {
919         index_count++;
920      }
921
922   }
923   while (index_count!=2);
924
925   if (fdc->upd765_command_bytes[4] != fdc->upd765_command_bytes[6])
926   {
927      /* no data - specified sector ID was not found */
928      fdc->upd765_status[1] |= UPD765_ST1_NO_DATA;
929   }
930
931   return 0;
932}
933
934static void upd765_read_complete(device_t *device)
935{
936   upd765_t *fdc = get_safe_token(device);
937/* causes problems!!! - need to fix */
938#ifdef NO_END_OF_CYLINDER
939   /* set end of cylinder */
940   fdc->upd765_status[1] &= ~UPD765_ST1_END_OF_CYLINDER;
941#else
942   /* completed read command */
943
944   /* end of cylinder is set when:
945        - a whole sector has been read
946        - terminal count input is not set
947        - AND the the sector specified by EOT was read
948        */
949
950   /* if end of cylinder is set, and we did receive a terminal count, then clear it */
951   if ((fdc->upd765_flags & UPD765_TC)!=0)
952   {
953      /* set end of cylinder */
954      fdc->upd765_status[1] &= ~UPD765_ST1_END_OF_CYLINDER;
955   }
956#endif
957
958   upd765_setup_st0(device);
959
960   fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
961   fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
962   fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
963   fdc->upd765_result_bytes[3] = fdc->upd765_command_bytes[2]; /* C */
964   fdc->upd765_result_bytes[4] = fdc->upd765_command_bytes[3]; /* H */
965   fdc->upd765_result_bytes[5] = fdc->upd765_command_bytes[4]; /* R */
966   fdc->upd765_result_bytes[6] = fdc->upd765_command_bytes[5]; /* N */
967
968   upd765_setup_result_phase(device,7);
969}
970
971static void upd765_read_data(device_t *device)
972{
973   upd765_t *fdc = get_safe_token(device);
974   device_t *img = current_image(device);
975
976   if (!upd765_get_rdy(device))
977   {
978      fdc->upd765_status[0] = 0x0c0 | (1<<4) | fdc->drive | (fdc->side<<2);
979      fdc->upd765_status[1] = 0x00;
980      fdc->upd765_status[2] = 0x00;
981
982      fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
983      fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
984      fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
985      fdc->upd765_result_bytes[3] = fdc->upd765_command_bytes[2]; /* C */
986      fdc->upd765_result_bytes[4] = fdc->upd765_command_bytes[3]; /* H */
987      fdc->upd765_result_bytes[5] = fdc->upd765_command_bytes[4]; /* R */
988      fdc->upd765_result_bytes[6] = fdc->upd765_command_bytes[5]; /* N */
989      upd765_setup_result_phase(device,7);
990      return;
991   }
992
993   if (LOG_VERBOSE)
994      logerror("sector c: %02x h: %02x r: %02x n: %02x\n",fdc->upd765_command_bytes[2], fdc->upd765_command_bytes[3],fdc->upd765_command_bytes[4], fdc->upd765_command_bytes[5]);
995
996   /* find a sector to read data from */
997   {
998      int found_sector_to_read;
999
1000      found_sector_to_read = 0;
1001      /* check for finished reading sectors */
1002      do
1003      {
1004         /* get matching sector */
1005         if (upd765_get_matching_sector(device))
1006         {
1007
1008            /* skip it? */
1009            if (upd765_read_skip_sector(device))
1010            {
1011               /* yes */
1012
1013               /* check that we haven't finished reading all sectors */
1014               if (upd765_sector_count_complete(device))
1015               {
1016                  /* read complete */
1017                  upd765_read_complete(device);
1018                  return;
1019               }
1020
1021               /* read not finished */
1022
1023               /* increment sector count */
1024               upd765_increment_sector(device);
1025            }
1026            else
1027            {
1028               /* found a sector to read */
1029               found_sector_to_read = 1;
1030            }
1031         }
1032         else
1033         {
1034            /* error in finding sector */
1035            upd765_read_complete(device);
1036            return;
1037         }
1038      }
1039      while (found_sector_to_read==0);
1040   }
1041
1042   {
1043      int data_size;
1044
1045      data_size = upd765_n_to_bytes(fdc->upd765_command_bytes[5]);
1046
1047      floppy_drive_read_sector_data(img, fdc->side, fdc->sector_id,fdc->data_buffer,data_size);
1048
1049        upd765_setup_execution_phase_read(device,fdc->data_buffer, data_size);
1050   }
1051}
1052
1053
1054static void upd765_format_track(device_t *device)
1055{
1056   upd765_t *fdc = get_safe_token(device);
1057   device_t *img = current_image(device);
1058
1059   /* write protected? */
1060   if (floppy_wpt_r(img) == CLEAR_LINE)
1061   {
1062      fdc->upd765_status[1] |= UPD765_ST1_NOT_WRITEABLE;
1063
1064      upd765_setup_st0(device);
1065      /* TODO: Check result is correct */
1066         fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1067            fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1068            fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1069         fdc->upd765_result_bytes[3] = fdc->format_data[0];
1070         fdc->upd765_result_bytes[4] = fdc->format_data[1];
1071         fdc->upd765_result_bytes[5] = fdc->format_data[2];
1072         fdc->upd765_result_bytes[6] = fdc->format_data[3];
1073         upd765_setup_result_phase(device,7);
1074
1075      return;
1076   }
1077
1078    upd765_setup_execution_phase_write(device, &fdc->format_data[0], 4);
1079}
1080
1081static void upd765_read_a_track(device_t *device)
1082{
1083   upd765_t *fdc = get_safe_token(device);
1084   int data_size;
1085
1086   /* SKIP not allowed with this command! */
1087
1088   /* get next id */
1089   chrn_id id;
1090
1091   upd765_get_next_id(device, &id);
1092
1093   /* TO BE CONFIRMED! */
1094   /* check id from disc */
1095   if (id.C==fdc->upd765_command_bytes[2])
1096   {
1097      if (id.H==fdc->upd765_command_bytes[3])
1098      {
1099         if (id.R==fdc->upd765_command_bytes[4])
1100         {
1101            if (id.N==fdc->upd765_command_bytes[5])
1102            {
1103               /* if ID found, then no data is not set */
1104               /* otherwise no data will remain set */
1105               fdc->upd765_status[1] &=~UPD765_ST1_NO_DATA;
1106            }
1107         }
1108      }
1109   }
1110
1111
1112   data_size = upd765_n_to_bytes(id.N);
1113
1114   floppy_drive_read_sector_data(current_image(device), fdc->side, fdc->sector_id,fdc->data_buffer,data_size);
1115
1116   upd765_setup_execution_phase_read(device,fdc->data_buffer, data_size);
1117}
1118
1119static int upd765_just_read_last_sector_on_track(device_t *device)
1120{
1121   if (floppy_drive_get_flag_state(current_image(device), FLOPPY_DRIVE_INDEX))
1122      return 1;
1123   return 0;
1124}
1125
1126static void upd765_write_complete(device_t *device)
1127{
1128   upd765_t *fdc = get_safe_token(device);
1129
1130/* causes problems!!! - need to fix */
1131#ifdef NO_END_OF_CYLINDER
1132        /* set end of cylinder */
1133        fdc->upd765_status[1] &= ~UPD765_ST1_END_OF_CYLINDER;
1134#else
1135   /* completed read command */
1136
1137   /* end of cylinder is set when:
1138     - a whole sector has been read
1139     - terminal count input is not set
1140     - AND the the sector specified by EOT was read
1141     */
1142
1143   /* if end of cylinder is set, and we did receive a terminal count, then clear it */
1144   if ((fdc->upd765_flags & UPD765_TC)!=0)
1145   {
1146      /* set end of cylinder */
1147      fdc->upd765_status[1] &= ~UPD765_ST1_END_OF_CYLINDER;
1148   }
1149#endif
1150
1151   upd765_setup_st0(device);
1152
1153    fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1154    fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1155    fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1156    fdc->upd765_result_bytes[3] = fdc->upd765_command_bytes[2]; /* C */
1157    fdc->upd765_result_bytes[4] = fdc->upd765_command_bytes[3]; /* H */
1158    fdc->upd765_result_bytes[5] = fdc->upd765_command_bytes[4]; /* R */
1159    fdc->upd765_result_bytes[6] = fdc->upd765_command_bytes[5]; /* N */
1160
1161    upd765_setup_result_phase(device,7);
1162}
1163
1164
1165static void upd765_write_data(device_t *device)
1166{
1167   upd765_t *fdc = get_safe_token(device);
1168   if (!upd765_get_rdy(device))
1169   {
1170      fdc->upd765_status[0] = 0x0c0 | (1<<4) | fdc->drive | (fdc->side<<2);
1171        fdc->upd765_status[1] = 0x00;
1172        fdc->upd765_status[2] = 0x00;
1173
1174        fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1175        fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1176        fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1177        fdc->upd765_result_bytes[3] = fdc->upd765_command_bytes[2]; /* C */
1178        fdc->upd765_result_bytes[4] = fdc->upd765_command_bytes[3]; /* H */
1179        fdc->upd765_result_bytes[5] = fdc->upd765_command_bytes[4]; /* R */
1180        fdc->upd765_result_bytes[6] = fdc->upd765_command_bytes[5]; /* N */
1181      upd765_setup_result_phase(device,7);
1182      return;
1183   }
1184
1185   /* write protected? */
1186   if (floppy_wpt_r(current_image(device)) == CLEAR_LINE)
1187   {
1188      fdc->upd765_status[1] |= UPD765_ST1_NOT_WRITEABLE;
1189
1190      upd765_write_complete(device);
1191      return;
1192   }
1193
1194   if (upd765_get_matching_sector(device))
1195   {
1196      int data_size;
1197
1198      data_size = upd765_n_to_bytes(fdc->upd765_command_bytes[5]);
1199
1200        upd765_setup_execution_phase_write(device,fdc->data_buffer, data_size);
1201   }
1202    else
1203    {
1204        upd765_setup_result_phase(device,7);
1205    }
1206}
1207
1208
1209/* return true if we have read all sectors, false if not */
1210static int upd765_sector_count_complete(device_t *device)
1211{
1212   upd765_t *fdc = get_safe_token(device);
1213/* this is not correct?? */
1214#if 1
1215   /* if terminal count has been set - yes */
1216   if (fdc->upd765_flags & UPD765_TC)
1217   {
1218      /* completed */
1219      return 1;
1220   }
1221
1222
1223
1224   /* multi-track? */
1225   if (fdc->upd765_command_bytes[0] & 0x080)
1226   {
1227      /* it appears that in multi-track mode,
1228        the EOT parameter of the command is ignored!? -
1229        or is it ignored the first time and not the next, so that
1230        if it is started on side 0, it will end at EOT on side 1,
1231        but if started on side 1 it will end at end of track????
1232
1233        PC driver requires this to end at last sector on side 1, and
1234        ignore EOT parameter.
1235
1236        To be checked!!!!
1237        */
1238
1239      /* if just read last sector and on side 1 - finish */
1240      if (upd765_just_read_last_sector_on_track(device))
1241      {
1242         if (floppy_get_heads_per_disk(flopimg_get_image(current_image(device)))==1) {
1243            return 1;
1244         } else {
1245            if (fdc->side==1)
1246               return 1;
1247         }
1248      }
1249
1250      /* if not on second side then we haven't finished yet */
1251      if (fdc->side!=1)
1252      {
1253         /* haven't finished yet */
1254         return 0;
1255      }
1256   }
1257   else
1258   {
1259      /* sector id == EOT? */
1260      if ((fdc->upd765_command_bytes[4]==fdc->upd765_command_bytes[6]))
1261      {
1262
1263         /* completed */
1264         return 1;
1265      }
1266   }
1267#else
1268
1269   /* if terminal count has been set - yes */
1270   if (fdc->upd765_flags & UPD765_TC)
1271   {
1272      /* completed */
1273      return 1;
1274   }
1275
1276   /* Multi-Track operation:
1277
1278    Verified on Amstrad CPC.
1279
1280        disc format used:
1281            9 sectors per track
1282            2 sides
1283            Sector IDs: &01, &02, &03, &04, &05, &06, &07, &08, &09
1284
1285        Command specified:
1286            SIDE = 0,
1287            C = 0,H = 0,R = 1, N = 2, EOT = 1
1288        Sectors read:
1289            Sector 1 side 0
1290            Sector 1 side 1
1291
1292        Command specified:
1293            SIDE = 0,
1294            C = 0,H = 0,R = 1, N = 2, EOT = 3
1295        Sectors read:
1296            Sector 1 side 0
1297            Sector 2 side 0
1298            Sector 3 side 0
1299            Sector 1 side 1
1300            Sector 2 side 1
1301            Sector 3 side 1
1302
1303
1304        Command specified:
1305            SIDE = 0,
1306            C = 0, H = 0, R = 7, N = 2, EOT = 3
1307        Sectors read:
1308            Sector 7 side 0
1309            Sector 8 side 0
1310            Sector 9 side 0
1311            Sector 10 not found. Error "No Data"
1312
1313        Command specified:
1314            SIDE = 1,
1315            C = 0, H = 1, R = 1, N = 2, EOT = 1
1316        Sectors read:
1317            Sector 1 side 1
1318
1319        Command specified:
1320            SIDE = 1,
1321            C = 0, H = 1, R = 1, N = 2, EOT = 2
1322        Sectors read:
1323            Sector 1 side 1
1324            Sector 1 side 2
1325
1326  */
1327
1328   /* sector id == EOT? */
1329   if ((fdc->upd765_command_bytes[4]==fdc->upd765_command_bytes[6]))
1330   {
1331      /* multi-track? */
1332      if (fdc->upd765_command_bytes[0] & 0x080)
1333      {
1334         /* if we have reached EOT (fdc->upd765_command_bytes[6])
1335            on side 1, then read is complete */
1336         if (fdc->side==1)
1337            return 1;
1338
1339         return 0;
1340
1341      }
1342
1343      /* completed */
1344      return 1;
1345   }
1346#endif
1347   /* not complete */
1348   return 0;
1349}
1350
1351static void   upd765_increment_sector(device_t *device)
1352{
1353   upd765_t *fdc = get_safe_token(device);
1354   /* multi-track? */
1355   if (fdc->upd765_command_bytes[0] & 0x080)
1356   {
1357      /* reached EOT? */
1358      /* if (fdc->upd765_command_bytes[4]==fdc->upd765_command_bytes[6])*/
1359      if (upd765_just_read_last_sector_on_track(device))
1360      {
1361         /* yes */
1362
1363         /* reached EOT */
1364         /* change side to 1 */
1365         fdc->side = 1;
1366         /* reset sector id to 1 */
1367         fdc->upd765_command_bytes[4] = 1;
1368         /* set head to 1 for get next sector test */
1369         fdc->upd765_command_bytes[3] = 1;
1370      }
1371      else
1372      {
1373         /* increment */
1374         fdc->upd765_command_bytes[4]++;
1375      }
1376
1377   }
1378   else
1379   {
1380      fdc->upd765_command_bytes[4]++;
1381   }
1382}
1383
1384/* control mark handling code */
1385
1386/* if SK==0, and we are executing a read data command, and a deleted data sector is found,
1387the data is not skipped. The data is read, but the control mark is set and the read is stopped */
1388/* if SK==0, and we are executing a read deleted data command, and a data sector is found,
1389the data is not skipped. The data is read, but the control mark is set and the read is stopped */
1390static int upd765_read_data_stop(device_t *device)
1391{
1392   upd765_t *fdc = get_safe_token(device);
1393   /* skip not set? */
1394   if ((fdc->upd765_command_bytes[0] & (1<<5))==0)
1395   {
1396      /* read data? */
1397      if (fdc->command == 0x06)
1398      {
1399         /* did we just read a sector with deleted data? */
1400         if (fdc->data_type == UPD765_DAM_DELETED_DATA)
1401         {
1402            /* set control mark */
1403            fdc->upd765_status[2] |= UPD765_ST2_CONTROL_MARK;
1404
1405            /* quit */
1406            return TRUE;
1407         }
1408      }
1409      /* deleted data? */
1410      else
1411      if (fdc->command == 0x0c)
1412      {
1413         /* did we just read a sector with data? */
1414         if (fdc->data_type == UPD765_DAM_DATA)
1415         {
1416            /* set control mark */
1417            fdc->upd765_status[2] |= UPD765_ST2_CONTROL_MARK;
1418
1419            /* quit */
1420            return TRUE;
1421         }
1422      }
1423   }
1424
1425   /* continue */
1426   return FALSE;
1427}
1428
1429static TIMER_CALLBACK(upd765_continue_command)
1430{
1431   device_t *device = (device_t *)ptr;
1432   upd765_t *fdc = get_safe_token(device);
1433   if ((fdc->upd765_phase == UPD765_EXECUTION_PHASE_READ) ||
1434      (fdc->upd765_phase == UPD765_EXECUTION_PHASE_WRITE))
1435   {
1436      switch (fdc->command)
1437        {
1438         /* read a track */
1439         case 0x02:
1440         {
1441            fdc->sector_counter++;
1442
1443            /* sector counter == EOT */
1444            if (fdc->sector_counter==fdc->upd765_command_bytes[6])
1445            {
1446               /* TODO: Add correct info here */
1447
1448               fdc->upd765_status[1] |= UPD765_ST1_END_OF_CYLINDER;
1449
1450               upd765_setup_st0(device);
1451
1452               fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1453               fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1454               fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1455               fdc->upd765_result_bytes[3] = fdc->upd765_command_bytes[2]; /* C */
1456               fdc->upd765_result_bytes[4] = fdc->upd765_command_bytes[3]; /* H */
1457               fdc->upd765_result_bytes[5] = fdc->upd765_command_bytes[4]; /* R */
1458               fdc->upd765_result_bytes[6] = fdc->upd765_command_bytes[5]; /* N */
1459
1460               upd765_setup_result_phase(device,7);
1461            }
1462            else
1463            {
1464               upd765_read_a_track(device);
1465            }
1466         }
1467         break;
1468
1469         /* format track */
1470         case 0x0d:
1471         {
1472            floppy_drive_format_sector(current_image(device), fdc->side, fdc->sector_counter,
1473               fdc->format_data[0], fdc->format_data[1],
1474               fdc->format_data[2], fdc->format_data[3],
1475               fdc->upd765_command_bytes[5]);
1476
1477            fdc->sector_counter++;
1478
1479            /* sector_counter = SC */
1480            if (fdc->sector_counter == fdc->upd765_command_bytes[3])
1481            {
1482               /* TODO: Check result is correct */
1483               fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1484               fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1485               fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1486               fdc->upd765_result_bytes[3] = fdc->format_data[0];
1487               fdc->upd765_result_bytes[4] = fdc->format_data[1];
1488               fdc->upd765_result_bytes[5] = fdc->format_data[2];
1489               fdc->upd765_result_bytes[6] = fdc->format_data[3];
1490               upd765_setup_result_phase(device,7);
1491            }
1492            else
1493            {
1494               upd765_format_track(device);
1495            }
1496         }
1497         break;
1498
1499         /* write data, write deleted data */
1500         case 0x09:
1501         case 0x05:
1502         {
1503            /* sector id == EOT */
1504            UINT8 ddam;
1505
1506            ddam = 0;
1507            if (fdc->command == 0x09)
1508            {
1509               ddam = 1;
1510            }
1511
1512            /* write data to disc */
1513            floppy_drive_write_sector_data(current_image(device), fdc->side, fdc->sector_id,fdc->data_buffer,upd765_n_to_bytes(fdc->upd765_command_bytes[5]),ddam);
1514
1515            if (upd765_sector_count_complete(device))
1516            {
1517               upd765_increment_sector(device);
1518               upd765_write_complete(device);
1519            }
1520            else
1521            {
1522               upd765_increment_sector(device);
1523               upd765_write_data(device);
1524            }
1525         }
1526         break;
1527
1528         /* read data, read deleted data */
1529         case 0x0c:
1530         case 0x06:
1531         {
1532
1533            /* read all sectors? */
1534
1535            /* sector id == EOT */
1536            if (upd765_sector_count_complete(device) || upd765_read_data_stop(device))
1537            {
1538               upd765_read_complete(device);
1539            }
1540            else
1541            {
1542               upd765_increment_sector(device);
1543               upd765_read_data(device);
1544            }
1545         }
1546         break;
1547
1548         default:
1549            break;
1550      }
1551   }
1552}
1553
1554
1555static int upd765_get_command_byte_count(device_t *device)
1556{
1557   upd765_t *fdc = get_safe_token(device);
1558   fdc->command = fdc->upd765_command_bytes[0] & 0x01f;
1559
1560   if (fdc->version==TYPE_UPD765A)
1561   {
1562       return upd765_cmd_size[fdc->command];
1563    }
1564   else
1565   {
1566      if (fdc->version==TYPE_UPD72065)
1567      {
1568         switch(fdc->upd765_command_bytes[0])
1569         {
1570            case 0x34: // Reset Standby
1571            case 0x35: // Set Standby
1572            case 0x36: // Software Reset
1573               return 1;
1574            default:
1575               return upd765_cmd_size[fdc->command];
1576         }
1577      }
1578      if (fdc->version==TYPE_SMC37C78)
1579      {
1580         switch (fdc->command)
1581         {
1582            /* version */
1583            case 0x010:
1584               return 1;
1585
1586            /* verify */
1587            case 0x016:
1588               return 9;
1589
1590            /* configure */
1591            case 0x013:
1592               return 4;
1593
1594            /* dumpreg */
1595            case 0x0e:
1596               return 1;
1597
1598            /* perpendicular mode */
1599            case 0x012:
1600               return 1;
1601
1602            /* lock */
1603            case 0x014:
1604               return 1;
1605
1606            /* seek/relative seek are together! */
1607
1608            default:
1609               return upd765_cmd_size[fdc->command];
1610         }
1611      }
1612   }
1613
1614   return upd765_cmd_size[fdc->command];
1615}
1616
1617
1618
1619
1620
1621void upd765_update_state(device_t *device)
1622{
1623   upd765_t *fdc = get_safe_token(device);
1624   switch (fdc->upd765_phase) {
1625   case UPD765_RESULT_PHASE:
1626      /* set data reg */
1627      fdc->upd765_data_reg = fdc->upd765_result_bytes[fdc->upd765_transfer_bytes_count];
1628
1629      if (fdc->upd765_transfer_bytes_count == 0)
1630      {
1631         /* clear int for specific commands */
1632         switch (fdc->command) {
1633         case 2:   /* read a track */
1634         case 5:   /* write data */
1635         case 6:   /* read data */
1636         case 9:   /* write deleted data */
1637         case 10:   /* read id */
1638         case 12:   /* read deleted data */
1639         case 13:   /* format at track */
1640         case 17:   /* scan equal */
1641         case 19:   /* scan low or equal */
1642         case 29:   /* scan high or equal */
1643            upd765_set_int(device, 0);
1644            break;
1645
1646         default:
1647            break;
1648         }
1649      }
1650
1651      if (LOG_VERBOSE)
1652         logerror("UPD765: RESULT: %02x\n", fdc->upd765_data_reg);
1653
1654      fdc->upd765_transfer_bytes_count++;
1655      fdc->upd765_transfer_bytes_remaining--;
1656
1657      if (fdc->upd765_transfer_bytes_remaining==0)
1658      {
1659         upd765_idle(device);
1660      }
1661      else
1662      {
1663         upd765_set_data_request(device);
1664      }
1665      break;
1666
1667   case UPD765_EXECUTION_PHASE_READ:
1668      /* setup data register */
1669      fdc->upd765_data_reg = fdc->execution_phase_data[fdc->upd765_transfer_bytes_count];
1670      fdc->upd765_transfer_bytes_count++;
1671      fdc->upd765_transfer_bytes_remaining--;
1672
1673      if (LOG_EXTRA)
1674         logerror("EXECUTION PHASE READ: %02x\n", fdc->upd765_data_reg);
1675
1676      if ((fdc->upd765_transfer_bytes_remaining==0) || (fdc->upd765_flags & UPD765_TC))
1677      {
1678         fdc->command_timer->adjust(attotime::zero);
1679      }
1680      else
1681      {
1682         /* trigger int */
1683         upd765_setup_timed_data_request(device,1);
1684      }
1685      break;
1686
1687   case UPD765_COMMAND_PHASE_FIRST_BYTE:
1688      fdc->FDC_main |= 0x10;                      /* set BUSY */
1689
1690      if (LOG_VERBOSE)
1691         logerror("%s: upd765(): command=0x%02x\n", device->machine().describe_context(), fdc->upd765_data_reg);
1692
1693      /* seek in progress? */
1694      if (fdc->upd765_flags & UPD765_SEEK_ACTIVE)
1695      {
1696         /* any command results in a invalid - I think that seek, recalibrate and
1697            sense interrupt status may work*/
1698         if (fdc->upd765_data_reg != 8)   /* Sense Interrupt Status */
1699            fdc->upd765_data_reg = 0;
1700      }
1701
1702      fdc->upd765_command_bytes[0] = fdc->upd765_data_reg;
1703
1704      fdc->upd765_transfer_bytes_remaining = upd765_get_command_byte_count(device);
1705
1706      fdc->upd765_transfer_bytes_count = 1;
1707      fdc->upd765_transfer_bytes_remaining--;
1708
1709      if (fdc->upd765_transfer_bytes_remaining==0)
1710      {
1711         upd765_setup_command(device);
1712      }
1713      else
1714      {
1715         /* request more data */
1716         upd765_set_data_request(device);
1717         fdc->upd765_phase = UPD765_COMMAND_PHASE_BYTES;
1718      }
1719        break;
1720
1721    case UPD765_COMMAND_PHASE_BYTES:
1722      if (LOG_VERBOSE)
1723         logerror("%s: upd765(): command=0x%02x\n", device->machine().describe_context(), fdc->upd765_data_reg);
1724
1725      fdc->upd765_command_bytes[fdc->upd765_transfer_bytes_count] = fdc->upd765_data_reg;
1726      fdc->upd765_transfer_bytes_count++;
1727      fdc->upd765_transfer_bytes_remaining--;
1728
1729      if (fdc->upd765_transfer_bytes_remaining==0)
1730      {
1731         upd765_setup_command(device);
1732      }
1733      else
1734      {
1735         /* request more data */
1736         upd765_set_data_request(device);
1737      }
1738      break;
1739
1740    case UPD765_EXECUTION_PHASE_WRITE:
1741      fdc->execution_phase_data[fdc->upd765_transfer_bytes_count]=fdc->upd765_data_reg;
1742      fdc->upd765_transfer_bytes_count++;
1743      fdc->upd765_transfer_bytes_remaining--;
1744
1745      if ((fdc->upd765_transfer_bytes_remaining == 0) || (fdc->upd765_flags & UPD765_TC))
1746      {
1747         fdc->command_timer->adjust(attotime::zero);
1748      }
1749      else
1750      {
1751         upd765_setup_timed_data_request(device,1);
1752      }
1753      break;
1754   }
1755}
1756
1757
1758READ8_DEVICE_HANDLER(upd765_data_r)
1759{
1760   upd765_t *fdc = get_safe_token(device);
1761
1762//  if ((fdc->FDC_main & 0x0c0) == 0x0c0)
1763   if ((fdc->FDC_main & 0x080) == 0x080)
1764   {
1765      if (
1766         (fdc->upd765_phase == UPD765_EXECUTION_PHASE_READ) ||
1767         (fdc->upd765_phase == UPD765_EXECUTION_PHASE_WRITE))
1768      {
1769
1770         /* reading the data byte clears the interrupt */
1771         upd765_set_int(device,CLEAR_LINE);
1772      }
1773
1774      /* reset data request */
1775      upd765_clear_data_request(device);
1776
1777      /* update state */
1778      upd765_update_state(device);
1779   }
1780
1781   if (LOG_EXTRA)
1782      logerror("DATA R: %02x\n", fdc->upd765_data_reg);
1783
1784   return fdc->upd765_data_reg;
1785}
1786
1787WRITE8_DEVICE_HANDLER(upd765_data_w)
1788{
1789   upd765_t *fdc = get_safe_token(device);
1790
1791   if (LOG_EXTRA)
1792      logerror("DATA W: %02x\n", data);
1793
1794   /* write data to data reg */
1795   fdc->upd765_data_reg = data;
1796
1797   if ((fdc->FDC_main & 0x0c0)==0x080)
1798   {
1799      if (
1800         (fdc->upd765_phase == UPD765_EXECUTION_PHASE_READ) ||
1801         (fdc->upd765_phase == UPD765_EXECUTION_PHASE_WRITE))
1802      {
1803
1804         /* reading the data byte clears the interrupt */
1805         upd765_set_int(device, CLEAR_LINE);
1806      }
1807
1808      /* reset data request */
1809      upd765_clear_data_request(device);
1810
1811      /* update state */
1812      upd765_update_state(device);
1813   }
1814}
1815
1816static void upd765_setup_invalid(device_t *device)
1817{
1818   upd765_t *fdc = get_safe_token(device);
1819
1820   fdc->command = 0;
1821   fdc->upd765_result_bytes[0] = 0x080;
1822   upd765_setup_result_phase(device,1);
1823}
1824
1825static void upd765_setup_command(device_t *device)
1826{
1827   upd765_t *fdc = get_safe_token(device);
1828
1829   static const char *const commands[] =
1830   {
1831      NULL,                  /* [00] */
1832      NULL,                  /* [01] */
1833      "Read Track",            /* [02] */
1834      "Specify",               /* [03] */
1835      "Sense Drive Status",      /* [04] */
1836      "Write Data",            /* [05] */
1837      "Read Data",            /* [06] */
1838      "Recalibrate",            /* [07] */
1839      "Sense Interrupt Status",   /* [08] */
1840      "Write Deleted Data",      /* [09] */
1841      "Read ID",               /* [0A] */
1842      NULL,                  /* [0B] */
1843      "Read Deleted Data",      /* [0C] */
1844      "Format Track",            /* [0D] */
1845      "Dump Registers",         /* [0E] */
1846      "Seek",                  /* [0F] */
1847      "Version",               /* [10] */
1848      NULL,                  /* [11] */
1849      "Perpendicular Mode",      /* [12] */
1850      "Configure",            /* [13] */
1851      "Lock"                  /* [14] */
1852   };
1853
1854   device_t *img;
1855   const char *cmd = NULL;
1856   chrn_id id;
1857
1858   /* if not in dma mode set execution phase bit */
1859   if (!(fdc->upd765_flags & UPD765_DMA_MODE))
1860   {
1861        fdc->FDC_main |= 0x020;              /* execution phase */
1862   }
1863
1864   if (LOG_COMMAND)
1865   {
1866      if ((fdc->upd765_command_bytes[0] & 0x1f) < ARRAY_LENGTH(commands))
1867         cmd = commands[fdc->upd765_command_bytes[0] & 0x1f];
1868      logerror("upd765_setup_command(): Setting up command 0x%02X (%s)\n",
1869         fdc->upd765_command_bytes[0] & 0x1f, cmd ? cmd : "???");
1870   }
1871
1872   switch (fdc->upd765_command_bytes[0] & 0x1f)
1873   {
1874      case 0x03:      /* specify */
1875         /* setup step rate */
1876         fdc->srt_in_ms = 16-((fdc->upd765_command_bytes[1]>>4) & 0x0f);
1877
1878         fdc->upd765_flags &= ~UPD765_DMA_MODE;
1879
1880         if ((fdc->upd765_command_bytes[2] & 0x01)==0)
1881         {
1882            fdc->upd765_flags |= UPD765_DMA_MODE;
1883         }
1884
1885         upd765_idle(device);
1886         break;
1887
1888      case 0x04:  /* sense drive status */
1889         upd765_setup_drive_and_side(device);
1890         img = current_image(device);
1891
1892         fdc->upd765_status[3] = fdc->drive | (fdc->side<<2);
1893
1894         if (img)
1895         {
1896            fdc->upd765_status[3] |= !floppy_tk00_r(img) << 4;
1897            fdc->upd765_status[3] |= !floppy_wpt_r(img) << 6;
1898
1899            if (upd765_get_rdy(device))
1900            {
1901               fdc->upd765_status[3] |= 0x20;
1902            }
1903         }
1904
1905         fdc->upd765_status[3] |= 0x08;
1906
1907         /* two side and fault not set but should be? */
1908         fdc->upd765_result_bytes[0] = fdc->upd765_status[3];
1909         upd765_setup_result_phase(device,1);
1910         break;
1911
1912      case 0x07:          /* recalibrate */
1913         upd765_seek_setup(device, 1);
1914         break;
1915
1916      case 0x0f:          /* seek */
1917         upd765_seek_setup(device, 0);
1918         break;
1919
1920      case 0x0a:      /* read id */
1921         upd765_setup_drive_and_side(device);
1922         img = current_image(device);
1923
1924         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
1925         fdc->upd765_status[1] = 0;
1926         fdc->upd765_status[2] = 0;
1927
1928         /* drive ready? */
1929         if (upd765_get_rdy(device))
1930         {
1931            /* is disk inserted? */
1932            device_image_interface *image = dynamic_cast<device_image_interface *>( img);
1933            if (image->exists())
1934            {
1935               int index_count = 0;
1936
1937               /* floppy drive is ready and disc is inserted */
1938
1939               /* this is the id that appears when a disc is not formatted */
1940               /* to be checked on Amstrad */
1941               id.C = 0;
1942               id.H = 0;
1943               id.R = 0x01;
1944               id.N = 0x02;
1945
1946               /* repeat for two index counts before quitting */
1947               do
1948               {
1949                  /* get next id from disc */
1950                  if (floppy_drive_get_next_id(img, fdc->side, &id))
1951                  {
1952                     /* got an id - quit */
1953                     break;
1954                  }
1955
1956                  if (floppy_drive_get_flag_state(img, FLOPPY_DRIVE_INDEX))
1957                  {
1958                     /* update index count */
1959                     index_count++;
1960                  }
1961               }
1962               while (index_count!=2);
1963
1964               /* at this point, we have seen a id or two index pulses have occured! */
1965               fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1966               fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1967               fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1968               fdc->upd765_result_bytes[3] = id.C; /* C */
1969               fdc->upd765_result_bytes[4] = id.H; /* H */
1970               fdc->upd765_result_bytes[5] = id.R; /* R */
1971               fdc->upd765_result_bytes[6] = id.N; /* N */
1972
1973               upd765_setup_result_phase(device,7);
1974            }
1975            else
1976            {
1977               /* floppy drive is ready, but no disc is inserted */
1978               /* this occurs on the PC */
1979               /* in this case, the command never quits! */
1980               /* there are no index pulses to stop the command! */
1981            }
1982         }
1983         else
1984         {
1985            /* what are id values when drive not ready? */
1986
1987            /* not ready, abnormal termination */
1988            fdc->upd765_status[0] |= (1<<3) | (1<<6);
1989            fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1990            fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1991            fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1992            fdc->upd765_result_bytes[3] = 0; /* C */
1993            fdc->upd765_result_bytes[4] = 0; /* H */
1994            fdc->upd765_result_bytes[5] = 0; /* R */
1995            fdc->upd765_result_bytes[6] = 0; /* N */
1996         }
1997         break;
1998
1999
2000      case 0x08: /* sense interrupt status */
2001         /* interrupt pending? */
2002         if (fdc->upd765_flags & UPD765_INT)
2003         {
2004            /* yes. Clear int */
2005            upd765_set_int(device, CLEAR_LINE);
2006
2007            /* clear drive seek bits */
2008            fdc->FDC_main &= ~(1 | 2 | 4 | 8);
2009
2010            /* return status */
2011            fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
2012            /* return pcn */
2013            fdc->upd765_result_bytes[1] = fdc->pcn[fdc->drive];
2014
2015            /* return result */
2016            upd765_setup_result_phase(device,2);
2017         }
2018         else
2019         {
2020            if(fdc->version == TYPE_UPD72065 && (fdc->FDC_main & 0x0f) == 0x00)
2021            {  // based on XM6
2022               upd765_setup_invalid(device);
2023            }
2024            else
2025            {
2026               /* no int */
2027               fdc->upd765_result_bytes[0] = 0x80;
2028               /* return pcn */
2029               fdc->upd765_result_bytes[1] = fdc->pcn[fdc->drive];
2030
2031               /* return result */
2032               upd765_setup_result_phase(device,2);
2033            }
2034         }
2035         break;
2036
2037      case 0x06:  /* read data */
2038         upd765_setup_drive_and_side(device);
2039
2040         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2041         fdc->upd765_status[1] = 0;
2042         fdc->upd765_status[2] = 0;
2043
2044         upd765_read_data(device);
2045         break;
2046
2047      case 0x0c:
2048         /* read deleted data */
2049         upd765_setup_drive_and_side(device);
2050
2051         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2052         fdc->upd765_status[1] = 0;
2053         fdc->upd765_status[2] = 0;
2054
2055         /* .. for now */
2056         upd765_read_data(device);
2057         break;
2058
2059      case 0x09:
2060         /* write deleted data */
2061         upd765_setup_drive_and_side(device);
2062
2063         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2064         fdc->upd765_status[1] = 0;
2065         fdc->upd765_status[2] = 0;
2066
2067         /* ... for now */
2068         upd765_write_data(device);
2069         break;
2070
2071      case 0x02:
2072         /* read a track */
2073         upd765_setup_drive_and_side(device);
2074         img = current_image(device);
2075         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2076         fdc->upd765_status[1] = 0;
2077         fdc->upd765_status[2] = 0;
2078
2079         fdc->upd765_status[0] |= UPD765_ST1_NO_DATA;
2080
2081         /* wait for index */
2082         do
2083         {
2084            /* get next id from disc */
2085            floppy_drive_get_next_id(img, fdc->side,&id);
2086         }
2087         while ((floppy_drive_get_flag_state(img, FLOPPY_DRIVE_INDEX))==0);
2088
2089         fdc->sector_counter = 0;
2090
2091         upd765_read_a_track(device);
2092         break;
2093
2094      case 0x05:  /* write data */
2095         upd765_setup_drive_and_side(device);
2096
2097         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2098         fdc->upd765_status[1] = 0;
2099         fdc->upd765_status[2] = 0;
2100
2101         upd765_write_data(device);
2102         break;
2103
2104      case 0x0d:   /* format a track */
2105         upd765_setup_drive_and_side(device);
2106
2107         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2108         fdc->upd765_status[1] = 0;
2109         fdc->upd765_status[2] = 0;
2110
2111         fdc->sector_counter = 0;
2112
2113         upd765_format_track(device);
2114         break;
2115
2116      default:   /* invalid */
2117         switch (fdc->version)
2118         {
2119            case TYPE_UPD765A:
2120               upd765_setup_invalid(device);
2121               break;
2122
2123            case TYPE_UPD765B:
2124               /* from upd765b data sheet */
2125               if ((fdc->upd765_command_bytes[0] & 0x01f)==0x010)
2126               {
2127                  /* version */
2128                  fdc->upd765_status[0] = 0x090;
2129                  fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
2130                  upd765_setup_result_phase(device,1);
2131               }
2132               break;
2133
2134            case TYPE_UPD72065:
2135               switch(fdc->upd765_command_bytes[0] & 0x3f)
2136               {
2137                  case 0x36:  // Software Reset
2138                     upd765_reset(device,0);
2139                     upd765_idle(device);
2140                     if(LOG_COMMAND)
2141                        logerror("upd72065: command - Software Reset\n");
2142                     break;
2143                  default:
2144                     upd765_setup_invalid(device);
2145                     break;
2146               }
2147               break;
2148
2149            case TYPE_SMC37C78:
2150               /* TO BE COMPLETED!!! !*/
2151               switch (fdc->upd765_command_bytes[0] & 0x1f)
2152               {
2153                  case 0x10:      /* version */
2154                     fdc->upd765_status[0] = 0x90;
2155                     fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
2156                     upd765_setup_result_phase(device,1);
2157                     break;
2158
2159                  case 0x13:      /* configure */
2160                     upd765_idle(device);
2161                     break;
2162
2163                  case 0x0e:      /* dump reg */
2164                     fdc->upd765_result_bytes[0] = fdc->pcn[0];
2165                     fdc->upd765_result_bytes[1] = fdc->pcn[1];
2166                     fdc->upd765_result_bytes[2] = fdc->pcn[2];
2167                     fdc->upd765_result_bytes[3] = fdc->pcn[3];
2168
2169                     upd765_setup_result_phase(device,10);
2170                     break;
2171
2172                  case 0x12:      /* perpendicular mode */
2173                     upd765_idle(device);
2174                     break;
2175
2176                  case 0x14:      /* lock */
2177                     upd765_setup_result_phase(device,1);
2178                     break;
2179               }
2180               break;
2181         }
2182   }
2183}
2184
2185
2186/* dma acknowledge write */
2187WRITE8_DEVICE_HANDLER(upd765_dack_w)
2188{
2189   /* clear request */
2190   upd765_set_dma_drq(device, CLEAR_LINE);
2191
2192   /* write data */
2193   upd765_data_w(device, offset, data);
2194}
2195
2196READ8_DEVICE_HANDLER(upd765_dack_r)
2197{
2198   /* clear data request */
2199   upd765_set_dma_drq(device,CLEAR_LINE);
2200
2201   /* read data */
2202   return upd765_data_r(device, offset);
2203}
2204
2205static TIMER_CALLBACK( interrupt_callback )
2206{
2207   device_t* device = (device_t*)ptr;
2208   upd765_set_int(device, 1);
2209}
2210
2211void upd765_reset(device_t *device, int offset)
2212{
2213   upd765_t *fdc = get_safe_token(device);
2214
2215   /* upd765 in idle state - ready to accept commands */
2216   upd765_idle(device);
2217
2218   /* set int low */
2219   upd765_set_int(device, 0);
2220   /* set dma drq output */
2221   upd765_set_dma_drq(device,0);
2222
2223   /* tandy 100hx assumes that after NEC is reset, it is in DMA mode */
2224   fdc->upd765_flags |= UPD765_DMA_MODE;
2225
2226   /* if ready input is set during reset generate an int */
2227   if (fdc->upd765_flags & UPD765_FDD_READY)
2228   {
2229      int i;
2230      int a_drive_is_ready;
2231
2232      fdc->upd765_status[0] = 0x080 | 0x040;
2233
2234      /* for the purpose of pc-xt. If any of the drives have a disk inserted,
2235        do not set not-ready - need to check with pc_fdc->c whether all drives
2236        are checked or only the drive selected with the drive select bits?? */
2237
2238      a_drive_is_ready = 0;
2239      for (i = 0; i < 4; i++)
2240      {
2241         if (fdc->intf->floppy_drive_tags[i]!=NULL)
2242         {
2243            device_t *img;
2244
2245            if (device->owner() != NULL)
2246               img = device->owner()->subdevice(fdc->intf->floppy_drive_tags[i]);
2247            else
2248               img = device->machine().device(fdc->intf->floppy_drive_tags[i]);
2249
2250            device_image_interface *image = dynamic_cast<device_image_interface *>( img);
2251            if (image->exists())
2252            {
2253               a_drive_is_ready = 1;
2254               break;
2255            }
2256         }
2257
2258      }
2259
2260      if (!a_drive_is_ready && fdc->intf->rdy_pin == UPD765_RDY_PIN_CONNECTED )
2261      {
2262         fdc->upd765_status[0] |= 0x08;
2263      }
2264
2265      device->machine().scheduler().timer_set(attotime::from_usec(5), FUNC(interrupt_callback),0,device);
2266   }
2267}
2268
2269WRITE_LINE_DEVICE_HANDLER( upd765_reset_w )
2270{
2271   upd765_t *fdc = get_safe_token(device);
2272
2273   int flags;
2274
2275   /* get previous reset state */
2276   flags = fdc->upd765_flags;
2277
2278   /* set new reset state */
2279   /* clear reset */
2280   fdc->upd765_flags &= ~UPD765_RESET;
2281
2282   /* reset */
2283   if (state)
2284   {
2285      fdc->upd765_flags |= UPD765_RESET;
2286
2287      upd765_set_int(device, 0);
2288   }
2289
2290   /* reset changed state? */
2291   if (((flags^fdc->upd765_flags) & UPD765_RESET)!=0)
2292   {
2293      /* yes */
2294
2295      /* no longer reset */
2296      if ((fdc->upd765_flags & UPD765_RESET)==0)
2297      {
2298         /* reset nec */
2299         upd765_reset(device, 0);
2300      }
2301   }
2302}
2303
2304
2305WRITE_LINE_DEVICE_HANDLER( upd765_ready_w )
2306{
2307   upd765_t *fdc = get_safe_token(device);
2308
2309   /* clear ready state */
2310   fdc->upd765_flags &= ~UPD765_FDD_READY;
2311
2312   if (state)
2313   {
2314      fdc->upd765_flags |= UPD765_FDD_READY;
2315   }
2316}
2317
2318
2319/* Device Interface */
2320
2321static void common_start(device_t *device, int device_type)
2322{
2323   upd765_t *fdc = get_safe_token(device);
2324   // validate arguments
2325
2326   assert(device != NULL);
2327   assert(device->tag() != NULL);
2328   assert(device->static_config() != NULL);
2329
2330   fdc->intf = (const upd765_interface*)device->static_config();
2331
2332   fdc->version = (UPD765_VERSION)device_type;
2333   fdc->timer = device->machine().scheduler().timer_alloc(FUNC(upd765_timer_callback), (void*)device);
2334   fdc->seek_timer = device->machine().scheduler().timer_alloc(FUNC(upd765_seek_timer_callback), (void*)device);
2335   fdc->command_timer = device->machine().scheduler().timer_alloc(FUNC(upd765_continue_command), (void*)device);
2336
2337   fdc->upd765_flags &= UPD765_FDD_READY;
2338   fdc->data_buffer = auto_alloc_array(device->machine(), char, 32*1024);
2339
2340   fdc->out_int_func.resolve(fdc->intf->out_int_func, *device);
2341   fdc->out_drq_func.resolve(fdc->intf->out_drq_func, *device);
2342
2343   // register for state saving
2344   //state_save_register_item(device->machine(), "upd765", device->tag(), 0, upd765->number);
2345}
2346
2347static DEVICE_START( upd765a )
2348{
2349   common_start(device, TYPE_UPD765A);
2350}
2351
2352static DEVICE_START( upd765b )
2353{
2354   common_start(device, TYPE_UPD765B);
2355}
2356
2357static DEVICE_START( smc37c78 )
2358{
2359   common_start(device, TYPE_SMC37C78);
2360}
2361
2362static DEVICE_START( upd72065 )
2363{
2364   common_start(device, TYPE_UPD72065);
2365}
2366
2367static DEVICE_RESET( upd765 )
2368{
2369   int i;
2370   upd765_t *fdc = get_safe_token(device);
2371   for (i = 0; i < 4; i++) {
2372      if (fdc->intf->floppy_drive_tags[i]!=NULL)
2373      {
2374         device_t *img;
2375
2376         if (device->owner() != NULL)
2377            img = device->owner()->subdevice(fdc->intf->floppy_drive_tags[i]);
2378         else
2379            img = device->machine().device(fdc->intf->floppy_drive_tags[i]);
2380
2381         floppy_drive_set_controller(img, device);
2382         floppy_drive_set_ready_state_change_callback(img, upd765_set_ready_change_callback);
2383      }
2384   }
2385
2386   upd765_reset(device,0);
2387}
2388
2389DEVICE_GET_INFO( upd765a )
2390{
2391   switch (state)
2392   {
2393      /* --- the following bits of info are returned as 64-bit signed integers --- */
2394      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(upd765_t);               break;
2395      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
2396
2397      /* --- the following bits of info are returned as pointers to data or functions --- */
2398      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(upd765a);   break;
2399      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
2400      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(upd765);   break;
2401
2402      /* --- the following bits of info are returned as NULL-terminated strings --- */
2403      case DEVINFO_STR_NAME:                     strcpy(info->s, "UPD765A");                  break;
2404      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "UPD765A");                  break;
2405      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                     break;
2406      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                     break;
2407      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright MESS Team");         break;
2408   }
2409}
2410
2411
2412DEVICE_GET_INFO( upd765b )
2413{
2414   switch (state)
2415   {
2416      /* --- the following bits of info are returned as NULL-terminated strings --- */
2417      case DEVINFO_STR_NAME:                     strcpy(info->s, "UPD765B");            break;
2418
2419      /* --- the following bits of info are returned as pointers to data or functions --- */
2420      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(upd765b);   break;
2421
2422      default:                              DEVICE_GET_INFO_CALL(upd765a);            break;
2423   }
2424}
2425
2426DEVICE_GET_INFO( smc37c78 )
2427{
2428   switch (state)
2429   {
2430      /* --- the following bits of info are returned as NULL-terminated strings --- */
2431      case DEVINFO_STR_NAME:                     strcpy(info->s, "SMC37C78");            break;
2432
2433      /* --- the following bits of info are returned as pointers to data or functions --- */
2434      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(smc37c78);   break;
2435
2436      default:                              DEVICE_GET_INFO_CALL(upd765a);            break;
2437   }
2438}
2439
2440DEVICE_GET_INFO( upd72065 )
2441{
2442   switch (state)
2443   {
2444      /* --- the following bits of info are returned as NULL-terminated strings --- */
2445      case DEVINFO_STR_NAME:                     strcpy(info->s, "UPD72065");            break;
2446
2447      /* --- the following bits of info are returned as pointers to data or functions --- */
2448      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(upd72065);   break;
2449
2450      default:                              DEVICE_GET_INFO_CALL(upd765a);            break;
2451   }
2452}
2453
2454DEFINE_LEGACY_DEVICE(UPD765A, upd765a);
2455DEFINE_LEGACY_DEVICE(UPD765B, upd765b);
2456DEFINE_LEGACY_DEVICE(SMC37C78, smc37c78);
2457DEFINE_LEGACY_DEVICE(UPD72065, upd72065);
src/emu/machine/upd765.h
r11473r11474
1/***************************************************************************
2
3    machine/upd765.h
4
5    Functions to emulate a NEC upd765/Intel 8272 compatible
6    floppy disk controller
7
8***************************************************************************/
9
10#ifndef __UPD765_H__
11#define __UPD765_H__
12
13#include "devcb.h"
14#include "imagedev/flopdrv.h"
15
16
17/***************************************************************************
18    MACROS
19***************************************************************************/
20
21DECLARE_LEGACY_DEVICE(UPD765A, upd765a);
22DECLARE_LEGACY_DEVICE(UPD765B, upd765b);
23DECLARE_LEGACY_DEVICE(SMC37C78, smc37c78);
24DECLARE_LEGACY_DEVICE(UPD72065, upd72065);
25
26/***************************************************************************
27    TYPE DEFINITIONS
28***************************************************************************/
29
30/* RDY pin connected state */
31typedef enum
32{
33   UPD765_RDY_PIN_NOT_CONNECTED = 0,
34   UPD765_RDY_PIN_CONNECTED = 1
35} UPD765_RDY_PIN;
36
37#define UPD765_DAM_DELETED_DATA 0x0f8
38#define UPD765_DAM_DATA 0x0fb
39
40typedef device_t *(*upd765_get_image_func)(device_t *device, int floppy_index);
41#define UPD765_GET_IMAGE(name)   device_t *name(device_t *device, int floppy_index )
42
43
44typedef struct upd765_interface
45{
46   /* interrupt issued */
47   devcb_write_line out_int_func;
48
49   /* dma data request */
50   devcb_write_line out_drq_func;
51
52   /* image lookup */
53   upd765_get_image_func get_image;
54
55   UPD765_RDY_PIN rdy_pin;
56
57   const char *floppy_drive_tags[4];
58} upd765_interface;
59
60
61/***************************************************************************
62    FUNCTION PROTOTYPES
63***************************************************************************/
64
65/* read of data register */
66READ8_DEVICE_HANDLER(upd765_data_r);
67/* write to data register */
68WRITE8_DEVICE_HANDLER(upd765_data_w);
69/* read of main status register */
70READ8_DEVICE_HANDLER(upd765_status_r);
71
72/* dma acknowledge with write */
73WRITE8_DEVICE_HANDLER(upd765_dack_w);
74/* dma acknowledge with read */
75READ8_DEVICE_HANDLER(upd765_dack_r);
76
77/* reset upd765 */
78void upd765_reset(device_t *device, int);
79
80/* reset pin of upd765 */
81WRITE_LINE_DEVICE_HANDLER(upd765_reset_w);
82
83/* set upd765 terminal count input state */
84WRITE_LINE_DEVICE_HANDLER(upd765_tc_w);
85
86/* set upd765 ready input*/
87WRITE_LINE_DEVICE_HANDLER(upd765_ready_w);
88
89void upd765_idle(device_t *device);
90
91/*********************/
92/* STATUS REGISTER 1 */
93
94/* this is set if a TC signal was not received after the sector data was read */
95#define UPD765_ST1_END_OF_CYLINDER (1<<7)
96/* this is set if the sector ID being searched for is not found */
97#define UPD765_ST1_NO_DATA (1<<2)
98/* set if disc is write protected and a write/format operation was performed */
99#define UPD765_ST1_NOT_WRITEABLE (1<<1)
100
101/*********************/
102/* STATUS REGISTER 2 */
103
104/* C parameter specified did not match C value read from disc */
105#define UPD765_ST2_WRONG_CYLINDER (1<<4)
106/* C parameter specified did not match C value read from disc, and C read from disc was 0x0ff */
107#define UPD765_ST2_BAD_CYLINDER (1<<1)
108/* this is set if the FDC encounters a Deleted Data Mark when executing a read data
109command, or FDC encounters a Data Mark when executing a read deleted data command */
110#define UPD765_ST2_CONTROL_MARK (1<<6)
111
112/***************************************************************************
113    DEVICE CONFIGURATION MACROS
114***************************************************************************/
115
116#define MCFG_UPD765A_ADD(_tag, _intrf) \
117   MCFG_DEVICE_ADD(_tag, UPD765A, 0) \
118   MCFG_DEVICE_CONFIG(_intrf)
119
120#define MCFG_UPD765A_MODIFY(_tag, _intrf) \
121  MCFG_DEVICE_MODIFY(_tag)         \
122  MCFG_DEVICE_CONFIG(_intrf)
123
124#define MCFG_UPD765A_REMOVE(_tag)      \
125  MCFG_DEVICE_REMOVE(_tag)
126
127#define MCFG_UPD765B_ADD(_tag, _intrf) \
128   MCFG_DEVICE_ADD(_tag, UPD765B, 0) \
129   MCFG_DEVICE_CONFIG(_intrf)
130
131#define MCFG_UPD765B_MODIFY(_tag, _intrf) \
132  MCFG_DEVICE_MODIFY(_tag)         \
133  MCFG_DEVICE_CONFIG(_intrf)
134
135#define MCFG_UPD765B_REMOVE(_tag)      \
136  MCFG_DEVICE_REMOVE(_tag)
137
138#define MCFG_SMC37C78_ADD(_tag, _intrf) \
139   MCFG_DEVICE_ADD(_tag, SMC37C78, 0) \
140   MCFG_DEVICE_CONFIG(_intrf)
141
142#define MCFG_SMC37C78_MODIFY(_tag, _intrf) \
143  MCFG_DEVICE_MODIFY(_tag)         \
144  MCFG_DEVICE_CONFIG(_intrf)
145
146#define MCFG_SMC37C78_REMOVE(_tag)      \
147  MCFG_DEVICE_REMOVE(_tag)
148
149#define MCFG_UPD72065_ADD(_tag, _intrf) \
150   MCFG_DEVICE_ADD(_tag, UPD72065, 0) \
151   MCFG_DEVICE_CONFIG(_intrf)
152
153#define MCFG_UPD72065_MODIFY(_tag, _intrf) \
154  MCFG_DEVICE_MODIFY(_tag)         \
155  MCFG_DEVICE_CONFIG(_intrf)
156
157#define MCFG_UPD72065_REMOVE(_tag)      \
158  MCFG_DEVICE_REMOVE(_tag)
159
160
161#endif /* __UPD765_H__ */
src/emu/machine/serial.c
r11473r11474
1/* internal serial transmission */
2
3/* select a file on host filesystem to transfer using serial method,
4setup serial interface software in driver and let the transfer begin */
5
6/* this is used in the Amstrad NC Notepad emulation */
7
8
9
10/*
11
12    the output starts at 1 level. It changes to 0 when the start bit has been transmitted.
13    This therefore signals that data is following.
14    When all data bits have been received, stop bits are transmitted with a value of 1.
15
16    msm8251 expects this in asynchronous mode:
17    packet format:
18
19    bit count           function        value
20    1                   start bit       0
21    note 1              data bits       x
22    note 2              parity bit      x
23    note 3              stop bits       1
24
25    Note:
26    1. Data size can be defined (usual value is 8)
27    2. Parity bit (if parity is set to odd or even). Value of bit
28        is defined by data parity.
29    3. There should be at least 1 stop bit.
30*/
31
32#include "emu.h"
33#include "serial.h"
34
35
36#define VERBOSE 0
37#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
38
39/* a read/write bit stream. used to transmit data and to receive data */
40typedef struct _data_stream data_stream;
41struct _data_stream
42{
43   /* pointer to buffer */
44   unsigned char *pData;
45   /* length of buffer */
46   unsigned long DataLength;
47
48   /* bit offset within current byte */
49   unsigned long BitCount;
50   /* byte offset within data */
51   unsigned long ByteCount;
52};
53
54typedef struct _serial_t serial_t;
55struct _serial_t
56{
57   /* transmit data bit-stream */
58   data_stream transmit;
59   /* receive data bit-stream */
60   data_stream receive;
61
62   /* register to receive data */
63   serial_receive_register   receive_reg;
64   /* register to transmit data */
65   serial_transmit_register transmit_reg;
66
67   /* connection to transmit/receive data over */
68   serial_connection connection;
69
70   /* data form to transmit/receive */
71   data_form form;
72
73   int transmit_state;
74
75   /* baud rate */
76   unsigned long BaudRate;
77
78   /* baud rate timer */
79   emu_timer   *timer;
80};
81
82INLINE serial_t *get_safe_token(device_t *device)
83{
84   assert(device != NULL);
85   //assert(device->type() == SERIAL);
86
87   return (serial_t *)downcast<legacy_device_base *>(device)->token();
88}
89
90
91/* the serial streams */
92static TIMER_CALLBACK(serial_device_baud_rate_callback);
93
94
95/*********************************************************/
96
97static unsigned char serial_parity_table[256];
98
99void serial_helper_setup(void)
100{
101   int i;
102
103   /* if sum of all bits in the byte is even, then the data
104    has even parity, otherwise it has odd parity */
105   for (i=0; i<256; i++)
106   {
107      int data;
108      int sum;
109      int b;
110
111      sum = 0;
112      data = i;
113
114      for (b=0; b<8; b++)
115      {
116         sum+=data & 0x01;
117
118         data = data>>1;
119      }
120
121      serial_parity_table[i] = sum & 0x01;
122   }
123}
124
125static unsigned char serial_helper_get_parity(unsigned char data)
126{
127   return serial_parity_table[data & 0x0ff];
128}
129
130static void serial_device_in_callback(running_machine &machine, int id, unsigned long status)
131{
132/*  serial_t *ser = get_safe_token(device);
133
134    ser->connection.input_state = status;*/
135}
136
137/***** SERIAL DEVICE ******/
138void serial_device_setup(device_t *device, int baud_rate, int num_data_bits, int stop_bit_count, int parity_code)
139{
140   serial_t *ser = get_safe_token(device);
141
142   ser->BaudRate = baud_rate;
143   ser->form.word_length = num_data_bits;
144   ser->form.stop_bit_count = stop_bit_count;
145   ser->form.parity = parity_code;
146   ser->timer = device->machine().scheduler().timer_alloc(FUNC(serial_device_baud_rate_callback), (void *)device);
147
148   serial_connection_init(device->machine(),&ser->connection);
149   serial_connection_set_in_callback(device->machine(),&ser->connection, serial_device_in_callback);
150
151   /* signal to other end it is clear to send! */
152   /* data is initially high state */
153   /* set /rts */
154   ser->connection.State |= SERIAL_STATE_RTS;
155   /* signal to other end data is ready to be accepted */
156   /* set /dtr */
157   ser->connection.State |= SERIAL_STATE_DTR;
158   set_out_data_bit(ser->connection.State, 1);
159   serial_connection_out(device->machine(),&ser->connection);
160   transmit_register_reset(&ser->transmit_reg);
161   receive_register_reset(&ser->receive_reg);
162   receive_register_setup(&ser->receive_reg, &ser->form);
163}
164
165
166unsigned long serial_device_get_state(device_t *device)
167{
168   serial_t *ser = get_safe_token(device);
169
170   return ser->connection.State;
171}
172
173void serial_device_set_transmit_state(device_t *device, int state)
174{
175   int previous_state;
176   serial_t *ser = get_safe_token(device);
177
178   previous_state = ser->transmit_state;
179
180   ser->transmit_state = state;
181
182   if ((state^previous_state)!=0)
183   {
184      if (state)
185      {
186         /* start timer */
187         ser->timer->adjust(attotime::zero, 0, attotime::from_hz(ser->BaudRate));
188      }
189      else
190      {
191         /* remove timer */
192         ser->timer->reset();
193      }
194   }
195
196}
197
198/* get a bit from input stream */
199static int data_stream_get_data_bit_from_data_byte(data_stream *stream)
200{
201   int data_bit;
202   int data_byte;
203
204   if (stream->ByteCount<stream->DataLength)
205   {
206      /* get data from buffer */
207      data_byte= stream->pData[stream->ByteCount];
208   }
209   else
210   {
211      /* over end of buffer, so return 0 */
212      data_byte= 0;
213   }
214
215   /* get bit from data */
216   data_bit = (data_byte>>(7-stream->BitCount)) & 0x01;
217
218   /* update bit count */
219   stream->BitCount++;
220   /* ripple overflow onto byte count */
221   stream->ByteCount+=stream->BitCount>>3;
222   /* lock bit count into range */
223   stream->BitCount &=0x07;
224
225   /* do not let it read over end of data */
226   if (stream->ByteCount>=stream->DataLength)
227   {
228      stream->ByteCount = stream->DataLength-1;
229   }
230
231   return data_bit;
232}
233
234
235void receive_register_setup(serial_receive_register *receive, data_form *data_form)
236{
237   receive->bit_count = data_form->word_length + data_form->stop_bit_count;
238
239   if (data_form->parity != SERIAL_PARITY_NONE)
240   {
241      receive->bit_count++;
242   }
243}
244
245
246/* this is generic code to be used in serial chip implementations */
247/* the majority of serial chips work in the same way and this code will work */
248/* for them */
249
250/* receive a bit */
251void receive_register_update_bit(serial_receive_register *receive, int bit)
252{
253   int previous_bit;
254
255   LOG(("receive register receive bit: %1x\n",bit));
256   previous_bit = receive->register_data & 1;
257
258   /* shift previous bit 7 out */
259   receive->register_data = receive->register_data<<1;
260   /* shift new bit in */
261   receive->register_data = (receive->register_data & 0xfffe) | bit;
262   /* update bit count received */
263   receive->bit_count_received++;
264
265   /* asyncrhonouse mode */
266   if (receive->flags & RECEIVE_REGISTER_WAITING_FOR_START_BIT)
267   {
268      /* the previous bit is stored in uart.receive char bit 0 */
269      /* has the bit state changed? */
270      if (((previous_bit ^ bit) & 0x01)!=0)
271      {
272         /* yes */
273         if (bit==0)
274         {
275            //logerror("receive register saw start bit\n");
276
277            /* seen start bit! */
278            /* not waiting for start bit now! */
279            receive->flags &=~RECEIVE_REGISTER_WAITING_FOR_START_BIT;
280            receive->flags |=RECEIVE_REGISTER_SYNCHRONISED;
281            /* reset bit count received */
282            receive->bit_count_received = 0;
283         }
284      }
285   }
286   else
287   if (receive->flags & RECEIVE_REGISTER_SYNCHRONISED)
288   {
289      /* received all bits? */
290      if (receive->bit_count_received==receive->bit_count)
291      {
292         receive->bit_count_received = 0;
293         receive->flags &=~RECEIVE_REGISTER_SYNCHRONISED;
294         receive->flags |= RECEIVE_REGISTER_WAITING_FOR_START_BIT;
295         //logerror("receive register full\n");
296         receive->flags |= RECEIVE_REGISTER_FULL;
297      }
298   }
299}
300
301void receive_register_reset(serial_receive_register *receive_reg)
302{
303   receive_reg->bit_count_received = 0;
304   receive_reg->flags &=~RECEIVE_REGISTER_FULL;
305   receive_reg->flags &=~RECEIVE_REGISTER_SYNCHRONISED;
306   receive_reg->flags |= RECEIVE_REGISTER_WAITING_FOR_START_BIT;
307}
308
309void receive_register_extract(serial_receive_register *receive_reg, data_form *data_form)
310{
311   unsigned long data_shift;
312   UINT8 data;
313
314   receive_register_reset(receive_reg);
315
316   data_shift = 0;
317
318   /* if parity is even or odd, there should be a parity bit in the stream! */
319   if (data_form->parity!=SERIAL_PARITY_NONE)
320   {
321      data_shift++;
322   }
323
324   data_shift+=data_form->stop_bit_count;
325
326   /* strip off stop bits and parity */
327   data = receive_reg->register_data>>data_shift;
328
329   /* mask off other bits so data byte has 0's in unused bits */
330   data = data & (0x0ff
331      >>
332      (8-(data_form->word_length)));
333
334   receive_reg->byte_received  = data;
335
336   /* parity enable? */
337   switch (data_form->parity)
338   {
339      case SERIAL_PARITY_NONE:
340         break;
341
342      /* check parity */
343      case SERIAL_PARITY_ODD:
344      case SERIAL_PARITY_EVEN:
345      {
346         //unsigned char computed_parity;
347         //unsigned char parity_received;
348
349         /* get state of parity bit received */
350         //parity_received = (receive_reg->register_data>>data_form->stop_bit_count) & 0x01;
351
352         /* compute parity for received bits */
353         //computed_parity = serial_helper_get_parity(data);
354
355         if (data_form->parity == SERIAL_PARITY_ODD)
356         {
357            /* odd parity */
358
359
360         }
361         else
362         {
363            /* even parity */
364
365
366         }
367
368      }
369      break;
370   }
371}
372
373/***** TRANSMIT REGISTER *****/
374
375void transmit_register_reset(serial_transmit_register *transmit_reg)
376{
377   transmit_reg->flags |=TRANSMIT_REGISTER_EMPTY;
378}
379
380/* used to construct data in stream format */
381static void transmit_register_add_bit(serial_transmit_register *transmit_reg, int bit)
382{
383   /* combine bit */
384   transmit_reg->register_data = transmit_reg->register_data<<1;
385   transmit_reg->register_data &=~1;
386   transmit_reg->register_data|=(bit & 0x01);
387   transmit_reg->bit_count++;
388}
389
390
391/* generate data in stream format ready for transfer */
392void transmit_register_setup(serial_transmit_register *transmit_reg, data_form *data_form,unsigned char data_byte)
393{
394   int i;
395   unsigned char transmit_data;
396
397
398   transmit_reg->bit_count_transmitted = 0;
399   transmit_reg->bit_count = 0;
400   transmit_reg->flags &=~TRANSMIT_REGISTER_EMPTY;
401
402   /* start bit */
403   transmit_register_add_bit(transmit_reg,0);
404
405   /* data bits */
406   transmit_data = data_byte;
407   for (i=0; i<data_form->word_length; i++)
408   {
409      int databit;
410
411      /* get bit from data */
412      databit = (transmit_data>>(data_form->word_length-1)) & 0x01;
413      /* add bit to formatted byte */
414      transmit_register_add_bit(transmit_reg, databit);
415      transmit_data = transmit_data<<1;
416   }
417
418   /* parity */
419   if (data_form->parity!=SERIAL_PARITY_NONE)
420   {
421      /* odd or even parity */
422      unsigned char parity;
423
424      /* get parity */
425      /* if parity = 0, data has even parity - i.e. there is an even number of one bits in the data */
426      /* if parity = 1, data has odd parity - i.e. there is an odd number of one bits in the data */
427      parity = serial_helper_get_parity(data_byte);
428
429      transmit_register_add_bit(transmit_reg, parity);
430   }
431
432   /* stop bit(s) */
433   for (i=0; i<data_form->stop_bit_count; i++)
434   {
435      transmit_register_add_bit(transmit_reg,1);
436   }
437}
438
439
440/* get a bit from the transmit register */
441static int transmit_register_get_data_bit(serial_transmit_register *transmit_reg)
442{
443   int bit;
444
445   bit = (transmit_reg->register_data>>
446      (transmit_reg->bit_count - 1 -
447      transmit_reg->bit_count_transmitted)) & 0x01;
448
449   transmit_reg->bit_count_transmitted++;
450
451   /* have all bits of this stream formatted byte been sent? */
452   if (transmit_reg->bit_count_transmitted==transmit_reg->bit_count)
453   {
454      /* yes - generate a new byte to send */
455      transmit_reg->flags |= TRANSMIT_REGISTER_EMPTY;
456   }
457
458   return bit;
459}
460
461
462void transmit_register_send_bit(running_machine &machine, serial_transmit_register *transmit_reg, serial_connection *connection)
463{
464   int data;
465
466   data = transmit_register_get_data_bit(transmit_reg);
467
468   /* set tx data bit */
469   set_out_data_bit(connection->State, data);
470
471   /* state out through connection */
472   serial_connection_out(machine, connection);
473}
474
475static void serial_protocol_none_sent_char(device_t *device)
476{
477   int i;
478   int bit;
479   unsigned char data_byte;
480   serial_t *ser = get_safe_token(device);
481
482   /* generate byte to transmit */
483   data_byte = 0;
484   for (i=0; i<ser->form.word_length; i++)
485   {
486      data_byte = data_byte<<1;
487      bit = data_stream_get_data_bit_from_data_byte(&ser->transmit);
488      data_byte = data_byte|bit;
489   }
490   /* setup register */
491   transmit_register_setup(&ser->transmit_reg,&ser->form, data_byte);
492
493   logerror("serial device transmitted char: %02x\n",data_byte);
494}
495
496static TIMER_CALLBACK(serial_device_baud_rate_callback)
497{
498   serial_t *ser = get_safe_token((device_t*)ptr);
499
500   /* receive data into receive register */
501   receive_register_update_bit(&ser->receive_reg, get_in_data_bit(ser->connection.input_state));
502
503   if (ser->receive_reg.flags & RECEIVE_REGISTER_FULL)
504   {
505      //logerror("SERIAL DEVICE\n");
506      receive_register_extract(&ser->receive_reg, &ser->form);
507
508      logerror("serial device receive char: %02x\n",ser->receive_reg.byte_received);
509   }
510
511   /* is transmit empty? */
512   if (ser->transmit_reg.flags & TRANSMIT_REGISTER_EMPTY)
513   {
514      /* char has been sent, execute callback */
515      serial_protocol_none_sent_char((device_t*)ptr);
516   }
517
518   /* other side says it is clear to send? */
519   if (ser->connection.input_state & SERIAL_STATE_CTS)
520   {
521      /* send bit */
522      transmit_register_send_bit(machine, &ser->transmit_reg, &ser->connection);
523   }
524}
525
526/* connect the specified connection to this serial device */
527void serial_device_connect(device_t *device, serial_connection *connection)
528{
529   serial_t *ser = get_safe_token(device);
530   serial_connection_link(device->machine(), connection, &ser->connection);
531}
532
533
534/* load image */
535static int serial_device_load_internal(device_image_interface &image, unsigned char **ptr, int *pDataSize)
536{
537   int datasize;
538   unsigned char *data;
539
540   /* get file size */
541   datasize = image.length();
542
543   if (datasize!=0)
544   {
545      /* malloc memory for this data */
546      data = (unsigned char *)malloc(datasize);
547
548      if (data!=NULL)
549      {
550         /* read whole file */
551         image.fread(data, datasize);
552
553         *ptr = data;
554         *pDataSize = datasize;
555
556         logerror("File loaded!\r\n");
557
558         /* ok! */
559         return 1;
560      }
561   }
562   return 0;
563}
564
565/* reset position in stream */
566static void data_stream_reset(data_stream *stream)
567{
568   /* reset byte offset */
569   stream->ByteCount= 0;
570   /* reset bit count */
571   stream->BitCount = 0;
572}
573
574/* free stream */
575static void data_stream_free(data_stream *stream)
576{
577   if (stream->pData!=NULL)
578   {
579      free(stream->pData);
580      stream->pData = NULL;
581   }
582   stream->DataLength = 0;
583}
584
585/* initialise stream */
586static void data_stream_init(data_stream *stream, unsigned char *pData, unsigned long DataLength)
587{
588   stream->pData = pData;
589   stream->DataLength = DataLength;
590   data_stream_reset(stream);
591}
592
593DEVICE_START(serial)
594{
595   //serial_t *ser = get_safe_token(device);
596}
597
598DEVICE_RESET(serial)
599{
600//  serial_t *ser = get_safe_token(device);
601}
602
603DEVICE_IMAGE_LOAD(serial)
604{
605   int data_length;
606   unsigned char *data;
607   device_t *device = &image.device();
608
609   serial_t *ser = get_safe_token(device);
610
611   /* load file and setup transmit data */
612   if (serial_device_load_internal(image, &data, &data_length))
613   {
614      data_stream_init(&ser->transmit, data, data_length);
615      return IMAGE_INIT_PASS;
616   }
617
618   return IMAGE_INIT_FAIL;
619}
620
621
622DEVICE_IMAGE_UNLOAD(serial)
623{
624   device_t *device = &image.device();
625   serial_t *ser = get_safe_token(device);
626
627   /* stop transmit */
628   serial_device_set_transmit_state(device, 0);
629   /* free streams */
630   data_stream_free(&ser->transmit);
631   data_stream_free(&ser->receive);
632}
633
634
635DEVICE_GET_INFO( serial )
636{
637   switch ( state )
638   {
639      case DEVINFO_INT_TOKEN_BYTES:            info->i = sizeof(serial_t);                        break;
640      case DEVINFO_INT_INLINE_CONFIG_BYTES:      info->i = 0;                                 break;
641      case DEVINFO_INT_IMAGE_TYPE:               info->i = IO_SERIAL;                                   break;
642      case DEVINFO_INT_IMAGE_READABLE:            info->i = 1;                                           break;
643      case DEVINFO_INT_IMAGE_WRITEABLE:         info->i = 1;                                           break;
644      case DEVINFO_INT_IMAGE_CREATABLE:          info->i = 1;                                           break;
645
646      case DEVINFO_FCT_START:                      info->start = DEVICE_START_NAME( serial );             break;
647      case DEVINFO_FCT_RESET:                  info->reset = DEVICE_RESET_NAME( serial );            break;
648      case DEVINFO_FCT_IMAGE_LOAD:              info->f = (genf *) DEVICE_IMAGE_LOAD_NAME( serial );   break;
649      case DEVINFO_FCT_IMAGE_UNLOAD:              info->f = (genf *) DEVICE_IMAGE_UNLOAD_NAME(serial );   break;
650      case DEVINFO_STR_NAME:                      strcpy( info->s, "Serial port");                       break;
651      case DEVINFO_STR_FAMILY:                    strcpy(info->s, "Serial port");                           break;
652      case DEVINFO_STR_IMAGE_FILE_EXTENSIONS:       strcpy(info->s, "");                                    break;
653      case DEVINFO_STR_VERSION:               strcpy(info->s, "1.0");                           break;
654      case DEVINFO_STR_SOURCE_FILE:            strcpy(info->s, __FILE__);                        break;
655      case DEVINFO_STR_CREDITS:               strcpy(info->s, "Copyright the MESS Team");          break;
656   }
657}
658
659/*******************************************************************************/
660/*******************************************************************************/
661/********* SERIAL CONNECTION ***********/
662
663
664/* this converts state at this end to a state the other end can accept */
665/* e.g. CTS at this end becomes RTS at other end.
666        RTS at this end becomes CTS at other end.
667        TX at this end becomes RX at other end.
668        RX at this end becomes TX at other end.
669        etc
670
671        The same thing is done inside the serial null-terminal lead */
672
673static unsigned long serial_connection_spin_bits(unsigned long input_status)
674{
675
676   return
677      /* cts -> rts */
678      (((input_status & 0x01)<<1) |
679      /* rts -> cts */
680      ((input_status>>1) & 0x01) |
681      /* dsr -> dtr */
682      (((input_status>>2) & 0x01)<<3) |
683      /* dtr -> dsr */
684      (((input_status>>3) & 0x01)<<2) |
685      /* rx -> tx */
686      (((input_status>>4) & 0x01)<<5) |
687      /* tx -> rx */
688      (((input_status>>5) & 0x01)<<4));
689}
690
691
692/* setup callbacks for connection */
693void serial_connection_init(running_machine &machine, serial_connection *connection)
694{
695   connection->out_callback = NULL;
696   connection->in_callback = NULL;
697}
698
699/* set callback which will be executed when in status has changed */
700void serial_connection_set_in_callback(running_machine &machine, serial_connection *connection, void (*in_cb)(running_machine &machine, int id, unsigned long status))
701{
702   connection->in_callback = in_cb;
703}
704
705/* output new state through connection */
706void serial_connection_out(running_machine &machine, serial_connection *connection)
707{
708   if (connection->out_callback!=NULL)
709   {
710      unsigned long state_at_other_end;
711
712      state_at_other_end = serial_connection_spin_bits(connection->State);
713
714      connection->out_callback(machine, connection->id, state_at_other_end);
715   }
716}
717
718/* join two serial connections together */
719void serial_connection_link(running_machine &machine, serial_connection *connection_a, serial_connection *connection_b)
720{
721   /* both connections should have their in connection setup! */
722   /* the in connection is the callback they use to update their state based
723    on the output from the other side */
724   connection_a->out_callback = connection_b->in_callback;
725   connection_b->out_callback = connection_a->in_callback;
726
727   /* let b know the state of a */
728   serial_connection_out(machine,connection_a);
729   /* let a know the state of b */
730   serial_connection_out(machine,connection_b);
731}
732
733DEFINE_LEGACY_IMAGE_DEVICE(SERIAL, serial);
src/emu/machine/serial.h
r11473r11474
1/*****************************************************************************
2 *
3 * machine/serial.h
4 *
5 * internal serial transmission
6 *
7 * This code is used to transmit a file stored on the host filesystem
8 * (e.g. PC harddrive) to an emulated system.
9 *
10 * The file is converted into a serial bit-stream which can be received
11 * by the emulated serial chip in the emulated system.
12 *
13 * The file can be transmitted using different protocols.
14 *
15 * A and B are two computers linked with a serial connection
16 * A and B can transmit and receive data, through the same connection
17 *
18 * These flags apply to A and B, and give the state of the input & output
19 * signals at each side.
20 *
21 ****************************************************************************/
22
23#ifndef SERIAL_H_
24#define SERIAL_H_
25
26
27/*
28    CTS = Clear to Send. (INPUT)
29    Other end of connection is ready to accept data
30
31
32    NOTE:
33
34      This output is active low on serial chips (e.g. 0 is CTS is set),
35      but here it is active high!
36*/
37#define SERIAL_STATE_CTS   0x0001
38
39/*
40    RTS = Request to Send. (OUTPUT)
41    This end is ready to send data, and requests if the other
42    end is ready to accept it
43
44    NOTE:
45
46      This output is active low on serial chips (e.g. 0 is RTS is set),
47      but here it is active high!
48*/
49#define SERIAL_STATE_RTS   0x0002
50
51/*
52    DSR = Data Set ready. (INPUT)
53    Other end of connection has data
54
55
56    NOTE:
57
58      This output is active low on serial chips (e.g. 0 is DSR is set),
59      but here it is active high!
60*/
61#define SERIAL_STATE_DSR   0x0004
62
63/*
64    DTR = Data terminal Ready. (OUTPUT)
65    TX contains new data.
66
67    NOTE:
68
69      This output is active low on serial chips (e.g. 0 is DTR is set),
70      but here it is active high!
71*/
72#define SERIAL_STATE_DTR   0x0008
73/* RX = Recieve data. (INPUT) */
74#define SERIAL_STATE_RX_DATA   0x00010
75/* TX = Transmit data. (OUTPUT) */
76#define SERIAL_STATE_TX_DATA   0x00020
77
78/* parity selections */
79/* if all the bits are added in a byte, if the result is:
80    even -> parity is even
81    odd -> parity is odd
82*/
83enum
84{
85   SERIAL_PARITY_NONE,      /* no parity. a parity bit will not be in the transmitted/received data */
86   SERIAL_PARITY_ODD,      /* odd parity */
87   SERIAL_PARITY_EVEN      /* even parity */
88};
89
90/* this macro is used to extract the received data from the status */
91#define get_in_data_bit(x) ((x & SERIAL_STATE_RX_DATA)>>4)
92
93/* this macro is used to set the transmitted data in the status */
94#define set_out_data_bit(x, data) \
95   x&=~SERIAL_STATE_TX_DATA; \
96   x|=(data<<5)
97
98
99/*******************************************************************************/
100/**** SERIAL CONNECTION ***/
101
102
103/* this structure represents a serial connection */
104typedef struct _serial_connection serial_connection;
105struct _serial_connection
106{
107   int id;
108   /* state of this side */
109   unsigned long State;
110
111   /* state of other side - store here */
112   unsigned long input_state;
113
114   /* this callback is executed when this side has refreshed it's state,
115    to let the other end know */
116   void   (*out_callback)(running_machine &machine, int id, unsigned long state);
117   /* this callback is executed when the other side has refreshed it's state,
118    to let the other end know */
119   void   (*in_callback)(running_machine &machine, int id, unsigned long state);
120};
121
122/*----------- defined in machine/serial.c -----------*/
123
124/* setup out and in callbacks */
125void serial_connection_init(running_machine &machine, serial_connection *connection);
126
127/* set callback which will be executed when in status has changed */
128void serial_connection_set_in_callback(running_machine &machine, serial_connection *connection, void (*in_cb)(running_machine &machine, int id, unsigned long status));
129
130/* output status, if callback is setup it will be executed with the new status */
131void serial_connection_out(running_machine &machine, serial_connection *connection);
132
133/* join two serial connections */
134void serial_connection_link(running_machine &machine, serial_connection *connection_a, serial_connection *connection_b);
135
136
137/*******************************************************************************/
138
139/* form of data being transmitted and received */
140typedef struct _data_form data_form;
141struct _data_form
142{
143   /* length of word in bits */
144   unsigned long word_length;
145   /* parity state */
146   unsigned long parity;
147   /* number of stop bits */
148   unsigned long stop_bit_count;
149};
150
151/*******************************************************************************/
152
153/*******************************************************************************/
154/**** RECEIVE AND TRANSMIT GENERIC CODE ****/
155
156/* this can be used by most of the serial chip implementations,
157because they all work in roughly the same way.
158There is generic code to send and receive data in the specified form */
159
160/* receive is waiting for start bit. The transition from high-low indicates
161start of start bit. This is used to synchronise with the data being transfered */
162#define RECEIVE_REGISTER_WAITING_FOR_START_BIT 0x01
163/* receive is synchronised with data, data bits will be clocked in */
164#define RECEIVE_REGISTER_SYNCHRONISED 0x02
165/* set if receive register has been filled */
166#define RECEIVE_REGISTER_FULL 0x04
167
168
169/* the receive register holds data in receive form! */
170/* this must be extracted to get the data byte received */
171typedef struct _serial_receive_register serial_receive_register;
172struct _serial_receive_register
173{
174   /* data */
175   unsigned long register_data;
176   /* flags */
177   unsigned long flags;
178   /* bit count received */
179   unsigned long bit_count_received;
180   /* length of data to receive - includes data bits, parity bit and stop bit */
181   unsigned long bit_count;
182
183   /* the byte of data received */
184   unsigned char byte_received;
185};
186
187void   receive_register_setup(serial_receive_register *receive, data_form *data_form);
188void   receive_register_update_bit(serial_receive_register *receive, int bit_state);
189void   receive_register_extract(serial_receive_register *receive_reg, data_form *data_form);
190void   receive_register_reset(serial_receive_register *receive_reg);
191
192/* the transmit register is the final stage
193in the serial transmit procedure */
194/* normally, data is written to the transmit reg,
195then it is assembled into transmit form and transmitted */
196/* the transmit register holds data in transmit form */
197
198/* register is empty and ready to be filled with data */
199#define TRANSMIT_REGISTER_EMPTY 0x0001
200
201typedef struct _serial_transmit_register serial_transmit_register;
202struct _serial_transmit_register
203{
204   /* data */
205   unsigned long register_data;
206   /* flags */
207   unsigned long flags;
208   /* number of bits transmitted */
209   unsigned long bit_count_transmitted;
210   /* length of data to send */
211   unsigned long bit_count;
212};
213
214/* setup transmit reg ready for transmit */
215void transmit_register_setup(serial_transmit_register *transmit_reg, data_form *data_form,unsigned char data_byte);
216void   transmit_register_send_bit(running_machine &machine, serial_transmit_register *transmit_reg, serial_connection *connection);
217void   transmit_register_reset(serial_transmit_register *transmit_reg);
218
219/*******************************************************************************/
220/**** SERIAL HELPER ****/
221
222void serial_helper_setup(void);
223
224/*******************************************************************************/
225/**** SERIAL DEVICE ****/
226
227unsigned long serial_device_get_state(device_t *device);
228
229/* connect this device to the emulated serial chip */
230/* id is the serial device to connect to */
231/* connection is the serial connection to connect to the serial device */
232void serial_device_connect(device_t *image, serial_connection *connection);
233
234DECLARE_LEGACY_IMAGE_DEVICE(SERIAL, serial);
235
236#define MCFG_SERIAL_ADD(_tag) \
237   MCFG_DEVICE_ADD(_tag, SERIAL, 0)
238
239DEVICE_START(serial);
240DEVICE_IMAGE_LOAD(serial);
241
242void serial_device_setup(device_t *image, int baud_rate, int num_data_bits, int stop_bit_count, int parity_code);
243
244/* set the transmit state of the serial device */
245void serial_device_set_transmit_state(device_t *image, int state);
246
247#endif /* SERIAL_H_ */
src/emu/machine/msm8251.c
r11473r11474
1/*********************************************************************
2
3    msm8251.c
4
5    MSM/Intel 8251 Universal Synchronous/Asynchronous Receiver Transmitter code
6
7*********************************************************************/
8
9#include "emu.h"
10#include "msm8251.h"
11
12
13/***************************************************************************
14    MACROS
15***************************************************************************/
16
17#define VERBOSE 0
18
19#define LOG(x)   do { if (VERBOSE) logerror x; } while (0)
20
21
22/***************************************************************************
23    TYPE DEFINITIONS
24***************************************************************************/
25
26typedef struct _msm8251_t msm8251_t;
27struct _msm8251_t
28{
29   devcb_resolved_read_line   in_rxd_func;
30   devcb_resolved_write_line   out_txd_func;
31   devcb_resolved_read_line   in_dsr_func;
32   devcb_resolved_write_line   out_dtr_func;
33   devcb_resolved_write_line   out_rts_func;
34   devcb_resolved_write_line   out_rxrdy_func;
35   devcb_resolved_write_line   out_txrdy_func;
36   devcb_resolved_write_line   out_txempty_func;
37   devcb_resolved_write_line   out_syndet_func;
38
39   /* flags controlling how msm8251_control_w operates */
40   UINT8 flags;
41   /* offset into sync_bytes used during sync byte transfer */
42   UINT8 sync_byte_offset;
43   /* number of sync bytes written so far */
44   UINT8 sync_byte_count;
45   /* the sync bytes written */
46   UINT8 sync_bytes[2];
47   /* status of msm8251 */
48   UINT8 status;
49   UINT8 command;
50   /* mode byte - bit definitions depend on mode - e.g. synchronous, asynchronous */
51   UINT8 mode_byte;
52
53   /* data being received */
54   UINT8 data;
55
56   /* receive reg */
57   serial_receive_register receive_reg;
58   /* transmit reg */
59   serial_transmit_register transmit_reg;
60
61   data_form form;
62
63   /* the serial connection that data is transfered over */
64   /* this is usually connected to the serial device */
65   serial_connection connection;
66};
67
68
69
70/***************************************************************************
71    PROTOTYPES
72***************************************************************************/
73
74static void msm8251_in_callback(running_machine &machine, int id, unsigned long state);
75static void msm8251_update_tx_empty(device_t *device);
76static void msm8251_update_tx_ready(device_t *device);
77
78
79
80/***************************************************************************
81    INLINE FUNCTIONS
82***************************************************************************/
83
84INLINE msm8251_t *get_token(device_t *device)
85{
86   assert(device != NULL);
87   assert(device->type() == MSM8251);
88
89   return (msm8251_t *) downcast<legacy_device_base *>(device)->token();
90}
91
92
93INLINE const msm8251_interface *get_interface(device_t *device)
94{
95   assert(device != NULL);
96   assert(device->type() == MSM8251);
97
98   return (const msm8251_interface *) device->static_config();
99}
100
101
102/***************************************************************************
103    GLOBAL VARIABLES
104***************************************************************************/
105
106const msm8251_interface default_msm8251_interface = { DEVCB_NULL, };
107
108
109/***************************************************************************
110    IMPLEMENTATION
111***************************************************************************/
112
113/*-------------------------------------------------
114    msm8251_in_callback
115-------------------------------------------------*/
116
117static void msm8251_in_callback(running_machine &machine, int id, unsigned long state)
118{
119   device_t *device;
120   msm8251_t *uart;
121   int changed;
122
123   /* NPW 29-Nov-2008 - These two lines are a hack and indicate why our "serial" infrastructure needs to be updated */
124   device = machine.device("uart");
125   uart = get_token(device);
126
127   changed = uart->connection.input_state^state;
128
129   uart->connection.input_state = state;
130
131   /* did cts change state? */
132   if (changed & SERIAL_STATE_CTS)
133   {
134      /* yes */
135      /* update tx ready */
136      /* msm8251_update_tx_ready(device); */
137   }
138}
139
140
141
142/*-------------------------------------------------
143    DEVICE_START( msm8251 )
144-------------------------------------------------*/
145
146static DEVICE_START( msm8251 )
147{
148   msm8251_t *uart = get_token(device);
149   const msm8251_interface *intf = get_interface(device);
150
151   serial_helper_setup();
152
153   // resolve callbacks
154   uart->out_rxrdy_func.resolve(intf->out_rxrdy_func, *device);
155   uart->out_txrdy_func.resolve(intf->out_txrdy_func, *device);
156   uart->out_txempty_func.resolve(intf->out_txempty_func, *device);
157
158   /* setup this side of the serial connection */
159   serial_connection_init(device->machine(),&uart->connection);
160   serial_connection_set_in_callback(device->machine(),&uart->connection, msm8251_in_callback);
161   uart->connection.input_state = 0;
162}
163
164
165
166/*-------------------------------------------------
167    msm8251_update_rx_ready
168-------------------------------------------------*/
169
170static void msm8251_update_rx_ready(device_t *device)
171{
172   msm8251_t *uart = get_token(device);
173   int state;
174
175   state = uart->status & MSM8251_STATUS_RX_READY;
176
177   /* masked? */
178   if ((uart->command & (1<<2))==0)
179   {
180      state = 0;
181   }
182
183   uart->out_rxrdy_func(state != 0);
184}
185
186
187
188/*-------------------------------------------------
189    msm8251_receive_clock
190-------------------------------------------------*/
191
192void msm8251_receive_clock(device_t *device)
193{
194   msm8251_t *uart = get_token(device);
195
196   /* receive enable? */
197   if (uart->command & (1<<2))
198   {
199      //logerror("MSM8251\n");
200      /* get bit received from other side and update receive register */
201      receive_register_update_bit(&uart->receive_reg, get_in_data_bit(uart->connection.input_state));
202
203      if (uart->receive_reg.flags & RECEIVE_REGISTER_FULL)
204      {
205         receive_register_extract(&uart->receive_reg, &uart->form);
206         msm8251_receive_character(device, uart->receive_reg.byte_received);
207      }
208   }
209}
210
211
212
213/*-------------------------------------------------
214    msm8251_transmit_clock
215-------------------------------------------------*/
216
217void msm8251_transmit_clock(device_t *device)
218{
219   msm8251_t *uart = get_token(device);
220
221   /* transmit enable? */
222   if (uart->command & (1<<0))
223   {
224
225      /* transmit register full? */
226      if ((uart->status & MSM8251_STATUS_TX_READY)==0)
227      {
228         /* if transmit reg is empty */
229         if ((uart->transmit_reg.flags & TRANSMIT_REGISTER_EMPTY)!=0)
230         {
231            /* set it up */
232            transmit_register_setup(&uart->transmit_reg, &uart->form, uart->data);
233            /* msm8251 transmit reg now empty */
234            uart->status |=MSM8251_STATUS_TX_EMPTY;
235            /* ready for next transmit */
236            uart->status |=MSM8251_STATUS_TX_READY;
237
238            msm8251_update_tx_empty(device);
239            msm8251_update_tx_ready(device);
240         }
241      }
242
243      /* if transmit is not empty... transmit data */
244      if ((uart->transmit_reg.flags & TRANSMIT_REGISTER_EMPTY)==0)
245      {
246   //      logerror("MSM8251\n");
247         transmit_register_send_bit(device->machine(),&uart->transmit_reg, &uart->connection);
248      }
249   }
250
251#if 0
252   /* hunt mode? */
253   /* after each bit has been shifted in, it is compared against the current sync byte */
254   if (uart->command & (1<<7))
255   {
256      /* data matches sync byte? */
257      if (uart->data == uart->sync_bytes[uart->sync_byte_offset])
258      {
259         /* sync byte matches */
260         /* update for next sync byte? */
261         uart->sync_byte_offset++;
262
263         /* do all sync bytes match? */
264         if (uart->sync_byte_offset == uart->sync_byte_count)
265         {
266            /* ent hunt mode */
267            uart->command &=~(1<<7);
268         }
269      }
270      else
271      {
272         /* if there is no match, reset */
273         uart->sync_byte_offset = 0;
274      }
275   }
276#endif
277}
278
279
280
281/*-------------------------------------------------
282    msm8251_update_tx_ready
283-------------------------------------------------*/
284
285static void msm8251_update_tx_ready(device_t *device)
286{
287   msm8251_t *uart = get_token(device);
288
289   /* clear tx ready state */
290   int tx_ready;
291
292   /* tx ready output is set if:
293        DB Buffer Empty &
294        CTS is set &
295        Transmit enable is 1
296    */
297
298   tx_ready = 0;
299
300   /* transmit enable? */
301   if ((uart->command & (1<<0))!=0)
302   {
303      /* other side has rts set (comes in as CTS at this side) */
304      if (uart->connection.input_state & SERIAL_STATE_CTS)
305      {
306         if (uart->status & MSM8251_STATUS_TX_EMPTY)
307         {
308            /* enable transfer */
309            tx_ready = 1;
310         }
311      }
312   }
313
314   uart->out_txrdy_func(tx_ready);
315}
316
317
318
319/*-------------------------------------------------
320    msm8251_update_tx_empty
321-------------------------------------------------*/
322
323static void msm8251_update_tx_empty(device_t *device)
324{
325   msm8251_t *uart = get_token(device);
326
327   if (uart->status & MSM8251_STATUS_TX_EMPTY)
328   {
329      /* tx is in marking state (high) when tx empty! */
330      set_out_data_bit(uart->connection.State, 1);
331      serial_connection_out(device->machine(),&uart->connection);
332   }
333
334   uart->out_txempty_func((uart->status & MSM8251_STATUS_TX_EMPTY) != 0);
335}
336
337
338
339/*-------------------------------------------------
340    DEVICE_RESET( msm8251 )
341-------------------------------------------------*/
342
343static DEVICE_RESET( msm8251 )
344{
345   msm8251_t *uart = get_token(device);
346
347   LOG(("MSM8251: Reset\n"));
348
349   /* what is the default setup when the 8251 has been reset??? */
350
351   /* msm8251 datasheet explains the state of tx pin at reset */
352   /* tx is set to 1 */
353   set_out_data_bit(uart->connection.State,1);
354
355   /* assumption, rts is set to 1 */
356   uart->connection.State &= ~SERIAL_STATE_RTS;
357   serial_connection_out(device->machine(), &uart->connection);
358
359   transmit_register_reset(&uart->transmit_reg);
360   receive_register_reset(&uart->receive_reg);
361   /* expecting mode byte */
362   uart->flags |= MSM8251_EXPECTING_MODE;
363   /* not expecting a sync byte */
364   uart->flags &= ~MSM8251_EXPECTING_SYNC_BYTE;
365
366   /* no character to read by cpu */
367   /* transmitter is ready and is empty */
368   uart->status = MSM8251_STATUS_TX_EMPTY | MSM8251_STATUS_TX_READY;
369   uart->mode_byte = 0;
370   uart->command = 0;
371
372   /* update tx empty pin output */
373   msm8251_update_tx_empty(device);
374   /* update rx ready pin output */
375   msm8251_update_rx_ready(device);
376   /* update tx ready pin output */
377   msm8251_update_tx_ready(device);
378}
379
380
381
382/*-------------------------------------------------
383    WRITE8_DEVICE_HANDLER(msm8251_control_w)
384-------------------------------------------------*/
385
386WRITE8_DEVICE_HANDLER(msm8251_control_w)
387{
388   msm8251_t *uart = get_token(device);
389
390   if (uart->flags & MSM8251_EXPECTING_MODE)
391   {
392      if (uart->flags & MSM8251_EXPECTING_SYNC_BYTE)
393      {
394         LOG(("MSM8251: Sync byte\n"));
395
396         LOG(("Sync byte: %02x\n", data));
397         /* store sync byte written */
398         uart->sync_bytes[uart->sync_byte_offset] = data;
399         uart->sync_byte_offset++;
400
401         if (uart->sync_byte_offset == uart->sync_byte_count)
402         {
403            /* finished transfering sync bytes, now expecting command */
404            uart->flags &= ~(MSM8251_EXPECTING_MODE | MSM8251_EXPECTING_SYNC_BYTE);
405            uart->sync_byte_offset = 0;
406         //  uart->status = MSM8251_STATUS_TX_EMPTY | MSM8251_STATUS_TX_READY;
407         }
408      }
409      else
410      {
411         LOG(("MSM8251: Mode byte\n"));
412
413         uart->mode_byte = data;
414
415         /* Synchronous or Asynchronous? */
416         if ((data & 0x03)!=0)
417         {
418            /*  Asynchronous
419
420                    bit 7,6: stop bit length
421                        0 = inhibit
422                        1 = 1 bit
423                        2 = 1.5 bits
424                        3 = 2 bits
425                    bit 5: parity type
426                        0 = parity odd
427                        1 = parity even
428                    bit 4: parity test enable
429                        0 = disable
430                        1 = enable
431                    bit 3,2: character length
432                        0 = 5 bits
433                        1 = 6 bits
434                        2 = 7 bits
435                        3 = 8 bits
436                    bit 1,0: baud rate factor
437                        0 = defines command byte for synchronous or asynchronous
438                        1 = x1
439                        2 = x16
440                        3 = x64
441                */
442
443            LOG(("MSM8251: Asynchronous operation\n"));
444
445            LOG(("Character length: %d\n", (((data>>2) & 0x03)+5)));
446
447            if (data & (1<<4))
448            {
449               LOG(("enable parity checking\n"));
450            }
451            else
452            {
453               LOG(("parity check disabled\n"));
454            }
455
456            if (data & (1<<5))
457            {
458               LOG(("even parity\n"));
459            }
460            else
461            {
462               LOG(("odd parity\n"));
463            }
464
465            {
466               UINT8 stop_bit_length;
467
468               stop_bit_length = (data>>6) & 0x03;
469
470               switch (stop_bit_length)
471               {
472                  case 0:
473                  {
474                     /* inhibit */
475                     LOG(("stop bit: inhibit\n"));
476                  }
477                  break;
478
479                  case 1:
480                  {
481                     /* 1 */
482                     LOG(("stop bit: 1 bit\n"));
483                  }
484                  break;
485
486                  case 2:
487                  {
488                     /* 1.5 */
489                     LOG(("stop bit: 1.5 bits\n"));
490                  }
491                  break;
492
493                  case 3:
494                  {
495                     /* 2 */
496                     LOG(("stop bit: 2 bits\n"));
497                  }
498                  break;
499               }
500            }
501
502            uart->form.word_length = ((data>>2) & 0x03)+5;
503            uart->form.parity = SERIAL_PARITY_NONE;
504            switch ((data>>6) & 0x03)
505            {
506               case 0:
507               case 1:
508                  uart->form.stop_bit_count =  1;
509                  break;
510               case 2:
511               case 3:
512                  uart->form.stop_bit_count =  2;
513                  break;
514            }
515            receive_register_setup(&uart->receive_reg, &uart->form);
516
517
518#if 0
519            /* data bits */
520            uart->receive_char_length = (((data>>2) & 0x03)+5);
521
522            if (data & (1<<4))
523            {
524               /* parity */
525               uart->receive_char_length++;
526            }
527
528            /* stop bits */
529            uart->receive_char_length++;
530
531            uart->receive_flags &=~MSM8251_TRANSFER_RECEIVE_SYNCHRONISED;
532            uart->receive_flags |= MSM8251_TRANSFER_RECEIVE_WAITING_FOR_START_BIT;
533#endif
534            /* not expecting mode byte now */
535            uart->flags &= ~MSM8251_EXPECTING_MODE;
536//              uart->status = MSM8251_STATUS_TX_EMPTY | MSM8251_STATUS_TX_READY;
537         }
538         else
539         {
540            /*  bit 7: Number of sync characters
541                        0 = 1 character
542                        1 = 2 character
543                    bit 6: Synchronous mode
544                        0 = Internal synchronisation
545                        1 = External synchronisation
546                    bit 5: parity type
547                        0 = parity odd
548                        1 = parity even
549                    bit 4: parity test enable
550                        0 = disable
551                        1 = enable
552                    bit 3,2: character length
553                        0 = 5 bits
554                        1 = 6 bits
555                        2 = 7 bits
556                        3 = 8 bits
557                    bit 1,0 = 0
558                */
559            LOG(("MSM8251: Synchronous operation\n"));
560
561            /* setup for sync byte(s) */
562            uart->flags |= MSM8251_EXPECTING_SYNC_BYTE;
563            uart->sync_byte_offset = 0;
564            if (data & 0x07)
565            {
566               uart->sync_byte_count = 1;
567            }
568            else
569            {
570               uart->sync_byte_count = 2;
571            }
572
573         }
574      }
575   }
576   else
577   {
578      /* command */
579      LOG(("MSM8251: Command byte\n"));
580
581      uart->command = data;
582
583      LOG(("Command byte: %02x\n", data));
584
585      if (data & (1<<7))
586      {
587         LOG(("hunt mode\n"));
588      }
589
590      if (data & (1<<5))
591      {
592         LOG(("/rts set to 0\n"));
593      }
594      else
595      {
596         LOG(("/rts set to 1\n"));
597      }
598
599      if (data & (1<<2))
600      {
601         LOG(("receive enable\n"));
602      }
603      else
604      {
605         LOG(("receive disable\n"));
606      }
607
608      if (data & (1<<1))
609      {
610         LOG(("/dtr set to 0\n"));
611      }
612      else
613      {
614         LOG(("/dtr set to 1\n"));
615      }
616
617      if (data & (1<<0))
618      {
619         LOG(("transmit enable\n"));
620      }
621      else
622      {
623         LOG(("transmit disable\n"));
624      }
625
626
627      /*  bit 7:
628                0 = normal operation
629                1 = hunt mode
630            bit 6:
631                0 = normal operation
632                1 = internal reset
633            bit 5:
634                0 = /RTS set to 1
635                1 = /RTS set to 0
636            bit 4:
637                0 = normal operation
638                1 = reset error flag
639            bit 3:
640                0 = normal operation
641                1 = send break character
642            bit 2:
643                0 = receive disable
644                1 = receive enable
645            bit 1:
646                0 = /DTR set to 1
647                1 = /DTR set to 0
648            bit 0:
649                0 = transmit disable
650                1 = transmit enable
651        */
652
653      uart->connection.State &=~SERIAL_STATE_RTS;
654      if (data & (1<<5))
655      {
656         /* rts set to 0 */
657         uart->connection.State |= SERIAL_STATE_RTS;
658      }
659
660      uart->connection.State &=~SERIAL_STATE_DTR;
661      if (data & (1<<1))
662      {
663         uart->connection.State |= SERIAL_STATE_DTR;
664      }
665
666      if ((data & (1<<0))==0)
667      {
668         /* held in high state when transmit disable */
669         set_out_data_bit(uart->connection.State,1);
670      }
671
672
673      /* refresh outputs */
674      serial_connection_out(device->machine(), &uart->connection);
675
676      if (data & (1<<4))
677      {
678         uart->status &= ~(MSM8251_STATUS_PARITY_ERROR | MSM8251_STATUS_OVERRUN_ERROR | MSM8251_STATUS_FRAMING_ERROR);
679      }
680
681      if (data & (1<<6))
682      {
683         device->reset();
684      }
685
686      msm8251_update_rx_ready(device);
687      msm8251_update_tx_ready(device);
688
689   }
690}
691
692
693
694/*-------------------------------------------------
695    READ8_DEVICE_HANDLER(msm8251_status_r)
696-------------------------------------------------*/
697
698READ8_DEVICE_HANDLER(msm8251_status_r)
699{
700   msm8251_t *uart = get_token(device);
701
702   LOG(("status: %02x\n", uart->status));
703   return uart->status;
704}
705
706
707
708/*-------------------------------------------------
709    WRITE8_DEVICE_HANDLER(msm8251_data_w)
710-------------------------------------------------*/
711
712WRITE8_DEVICE_HANDLER(msm8251_data_w)
713{
714   msm8251_t *uart = get_token(device);
715
716   uart->data = data;
717
718   logerror("write data: %02x\n",data);
719
720   /* writing clears */
721   uart->status &=~MSM8251_STATUS_TX_READY;
722
723   /* if transmitter is active, then tx empty will be signalled */
724
725   msm8251_update_tx_ready(device);
726}
727
728
729
730/*-------------------------------------------------
731    msm8251_receive_character - called when last
732    bit of data has been received
733-------------------------------------------------*/
734
735void msm8251_receive_character(device_t *device, UINT8 ch)
736{
737   msm8251_t *uart = get_token(device);
738
739   logerror("msm8251 receive char: %02x\n",ch);
740
741   uart->data = ch;
742
743   /* char has not been read and another has arrived! */
744   if (uart->status & MSM8251_STATUS_RX_READY)
745   {
746      uart->status |= MSM8251_STATUS_OVERRUN_ERROR;
747   }
748   uart->status |= MSM8251_STATUS_RX_READY;
749
750   msm8251_update_rx_ready(device);
751}
752
753
754
755/*-------------------------------------------------
756    READ8_DEVICE_HANDLER(msm8251_data_r) - read data
757-------------------------------------------------*/
758
759READ8_DEVICE_HANDLER(msm8251_data_r)
760{
761   msm8251_t *uart = get_token(device);
762
763   logerror("read data: %02x, STATUS=%02x\n",uart->data,uart->status);
764   /* reading clears */
765   uart->status &= ~MSM8251_STATUS_RX_READY;
766
767   msm8251_update_rx_ready(device);
768   return uart->data;
769}
770
771
772
773/*-------------------------------------------------
774    msm8251_connect_to_serial_device - initialise
775    transfer using serial device - set the callback
776    which will be called when serial device has
777    updated it's state
778-------------------------------------------------*/
779
780void msm8251_connect_to_serial_device(device_t *device, device_t *image)
781{
782   msm8251_t *uart = get_token(device);
783   serial_device_connect(image, &uart->connection);
784}
785
786
787
788/*-------------------------------------------------
789    msm8251_connect
790-------------------------------------------------*/
791
792void msm8251_connect(device_t *device, serial_connection *other_connection)
793{
794   msm8251_t *uart = get_token(device);
795   serial_connection_link(device->machine(), &uart->connection, other_connection);
796}
797
798
799/*-------------------------------------------------
800    DEVICE_GET_INFO( msm8251 )
801-------------------------------------------------*/
802
803DEVICE_GET_INFO( msm8251 )
804{
805   switch (state)
806   {
807      /* --- the following bits of info are returned as 64-bit signed integers --- */
808      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(msm8251_t);            break;
809      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
810
811      /* --- the following bits of info are returned as pointers to data or functions --- */
812      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(msm8251);   break;
813      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
814      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(msm8251);   break;
815
816      /* --- the following bits of info are returned as NULL-terminated strings --- */
817      case DEVINFO_STR_NAME:                     strcpy(info->s, "Intel 8251 UART");            break;
818      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "Intel 8251 UART");            break;
819      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                     break;
820      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                     break;
821      case DEVINFO_STR_CREDITS:                  /* Nothing */                        break;
822   }
823}
824
825DEFINE_LEGACY_DEVICE(MSM8251, msm8251);
src/emu/machine/msm8251.h
r11473r11474
1/*********************************************************************
2
3    msm8251.h
4
5    MSM/Intel 8251 Universal Synchronous/Asynchronous Receiver Transmitter code
6
7*********************************************************************/
8
9#ifndef __MSM8251_H__
10#define __MSM8251_H__
11
12#include "machine/serial.h"
13
14
15/***************************************************************************
16    CONSTANTS
17***************************************************************************/
18
19DECLARE_LEGACY_DEVICE(MSM8251, msm8251);
20
21#define MSM8251_EXPECTING_MODE 0x01
22#define MSM8251_EXPECTING_SYNC_BYTE 0x02
23
24#define MSM8251_STATUS_FRAMING_ERROR 0x020
25#define MSM8251_STATUS_OVERRUN_ERROR 0x010
26#define MSM8251_STATUS_PARITY_ERROR 0x08
27#define MSM8251_STATUS_TX_EMPTY      0x04
28#define MSM8251_STATUS_RX_READY   0x02
29#define MSM8251_STATUS_TX_READY   0x01
30
31#define MCFG_MSM8251_ADD(_tag, _intrf) \
32   MCFG_DEVICE_ADD(_tag, MSM8251, 0) \
33   MCFG_DEVICE_CONFIG(_intrf)
34
35#define MCFG_MSM8251_REMOVE(_tag) \
36   MCFG_DEVICE_REMOVE(_tag)
37
38
39/***************************************************************************
40    TYPE DEFINITIONS
41***************************************************************************/
42
43typedef struct _msm8251_interface msm8251_interface;
44struct _msm8251_interface
45{
46   devcb_read_line      in_rxd_func;
47   devcb_write_line   out_txd_func;
48   devcb_read_line      in_dsr_func;
49   devcb_write_line   out_dtr_func;
50   devcb_write_line   out_rts_func;
51   devcb_write_line   out_rxrdy_func;
52   devcb_write_line   out_txrdy_func;
53   devcb_write_line   out_txempty_func;
54   devcb_write_line   out_syndet_func;
55};
56
57
58/***************************************************************************
59    PROTOTYPES
60***************************************************************************/
61
62extern const msm8251_interface default_msm8251_interface;
63
64/* read data register */
65READ8_DEVICE_HANDLER(msm8251_data_r);
66
67/* read status register */
68READ8_DEVICE_HANDLER(msm8251_status_r);
69
70/* write data register */
71WRITE8_DEVICE_HANDLER(msm8251_data_w);
72
73/* write control word */
74WRITE8_DEVICE_HANDLER(msm8251_control_w);
75
76/* The 8251 has seperate transmit and receive clocks */
77/* use these two functions to update the msm8251 for each clock */
78/* on NC100 system, the clocks are the same */
79void msm8251_transmit_clock(device_t *device);
80void msm8251_receive_clock(device_t *device);
81
82/* connecting to serial output */
83void msm8251_connect_to_serial_device(device_t *device, device_t *image);
84void msm8251_connect(device_t *device, serial_connection *other_connection);
85
86void msm8251_receive_character(device_t *device, UINT8 ch);
87
88#endif /* __MSM8251_H__ */
src/mess/machine/upd765.c
r0r11474
1/***************************************************************************
2
3    machine/upd765.c
4
5    Functions to emulate a UPD765/Intel 8272 compatible floppy disk controller
6
7    Code by Kevin Thacker.
8
9    TODO:
10
11    - overrun condition
12    - Scan Commands
13    - crc error in id field and crc error in data field errors
14    - disc not present, and no sectors on track for data, deleted data, write, write deleted,
15        read a track etc
16        - end of cylinder condition - almost working, needs fixing  with
17                PCW and PC drivers
18    - resolve "ready" state stuff (ready state when reset for PC, ready state change while processing command AND
19    while idle)
20
21    Changes:
22    091006 (Mariusz Wojcieszek, changes needed by QX-10):
23    - allowed "Sense Interrupt Status" command when Seek is active
24    - DIO bit in status register (0x40) is cleared when "Read Data" command is executed,
25      it is later set during result phase.
26
27***************************************************************************/
28
29#include "emu.h"
30#include "machine/upd765.h"
31
32
33typedef enum
34{
35   UPD765_COMMAND_PHASE_FIRST_BYTE,
36   UPD765_COMMAND_PHASE_BYTES,
37   UPD765_RESULT_PHASE,
38   UPD765_EXECUTION_PHASE_READ,
39   UPD765_EXECUTION_PHASE_WRITE
40} UPD765_PHASE;
41
42/* supported versions */
43typedef enum
44{
45   TYPE_UPD765A = 0,
46   TYPE_UPD765B = 1,
47   TYPE_SMC37C78 = 2,
48   TYPE_UPD72065 = 3
49} UPD765_VERSION;
50
51
52/* uncomment the following line for verbose information */
53#define LOG_VERBOSE      0
54#define LOG_COMMAND      0
55#define LOG_EXTRA      0
56#define LOG_INTERRUPT   0
57
58/* uncomment this to not allow end of cylinder "error" */
59#define NO_END_OF_CYLINDER
60
61
62
63/* state of upd765 Interrupt (INT) output */
64#define UPD765_INT   0x02
65/* data rate for floppy discs (MFM data) */
66#define UPD765_DATA_RATE   32
67/* state of upd765 terminal count input*/
68#define UPD765_TC   0x04
69
70#define UPD765_DMA_MODE 0x08
71
72#define UPD765_SEEK_OPERATION_IS_RECALIBRATE 0x01
73
74#define UPD765_SEEK_ACTIVE 0x010
75/* state of upd765 DMA DRQ output */
76#define UPD765_DMA_DRQ 0x020
77/* state of upd765 FDD READY input */
78#define UPD765_FDD_READY 0x040
79
80#define UPD765_MF   0x40
81
82#define UPD765_RESET 0x080
83
84typedef struct _upd765_t upd765_t;
85struct _upd765_t
86{
87   devcb_resolved_write_line   out_int_func;
88   devcb_resolved_write_line   out_drq_func;
89
90   unsigned long   sector_counter;
91   /* version of fdc to emulate */
92   UPD765_VERSION version;
93
94   /* main status register */
95   unsigned char    FDC_main;
96   /* data register */
97   unsigned char   upd765_data_reg;
98
99   unsigned char c,h,r,n;
100
101   int sector_id;
102
103   int data_type;
104
105   char format_data[4];
106
107   UPD765_PHASE    upd765_phase;
108   unsigned int    upd765_command_bytes[16];
109   unsigned int    upd765_result_bytes[16];
110   unsigned int    upd765_transfer_bytes_remaining;
111   unsigned int    upd765_transfer_bytes_count;
112   unsigned int    upd765_status[4];
113   /* present cylinder number per drive */
114   unsigned int    pcn[4];
115
116   /* drive being accessed. drive outputs from fdc */
117   unsigned int    drive;
118   /* side being accessed: side output from fdc */
119   unsigned int   side;
120
121
122   /* step rate time in us */
123   unsigned long   srt_in_ms;
124
125   unsigned int   ncn;
126
127//  unsigned int    upd765_id_index;
128   char *execution_phase_data;
129   unsigned int   upd765_flags;
130
131//  unsigned char specify[2];
132//  unsigned char perpendicular_mode[1];
133
134   int command;
135
136   emu_timer *seek_timer;
137   emu_timer *timer;
138   int timer_type;
139
140   emu_timer *command_timer;
141
142   char *data_buffer;
143   const upd765_interface *intf;
144};
145
146//static void upd765_setup_data_request(unsigned char Data);
147static void upd765_setup_command(device_t *device);
148static TIMER_CALLBACK(upd765_continue_command);
149static int upd765_sector_count_complete(device_t *device);
150static void upd765_increment_sector(device_t *device);
151static void upd765_update_state(device_t *device);
152static void upd765_set_dma_drq(device_t *device,int state);
153static void upd765_set_int(device_t *device,int state);
154
155static const INT8 upd765_cmd_size[32] =
156{
157   1,1,9,3,2,9,9,2,1,9,2,1,9,6,1,3,
158   1,9,1,1,1,1,9,1,1,9,1,1,1,9,1,1
159};
160
161INLINE upd765_t *get_safe_token(device_t *device)
162{
163   assert(device != NULL);
164   assert(device->type() == UPD765A || device->type() == UPD765B ||
165      device->type() == SMC37C78 || device->type() == UPD72065);
166
167   return (upd765_t *)downcast<legacy_device_base *>(device)->token();
168}
169
170static device_t *current_image(device_t *device)
171{
172   device_t *image = NULL;
173   upd765_t *fdc = get_safe_token(device);
174
175   if (!fdc->intf->get_image)
176   {
177      if (fdc->intf->floppy_drive_tags[fdc->drive] != NULL)
178      {
179         if (device->owner() != NULL)
180            image = device->owner()->subdevice(fdc->intf->floppy_drive_tags[fdc->drive]);
181         else
182            image = device->machine().device(fdc->intf->floppy_drive_tags[fdc->drive]);
183      }
184   }
185   else
186   {
187      image = fdc->intf->get_image(device, fdc->drive);
188   }
189   return image;
190}
191
192static void upd765_setup_drive_and_side(device_t *device)
193{
194   upd765_t *fdc = get_safe_token(device);
195   /* drive index upd765 sees */
196   fdc->drive = fdc->upd765_command_bytes[1] & 0x03;
197   /* side index upd765 sees */
198   fdc->side = (fdc->upd765_command_bytes[1]>>2) & 0x01;
199}
200
201
202/* setup status register 0 based on data in status register 1 and 2 */
203static void upd765_setup_st0(device_t *device)
204{
205   upd765_t *fdc = get_safe_token(device);
206   /* clear completition status bits, drive bits and side bits */
207   fdc->upd765_status[0] &= ~((1<<7) | (1<<6) | (1<<2) | (1<<1) | (1<<0));
208   /* fill in drive */
209   fdc->upd765_status[0] |= fdc->drive | (fdc->side<<2);
210
211   /* fill in completion status bits based on bits in st0, st1, st2 */
212   /* no error bits set */
213   if ((fdc->upd765_status[1] | fdc->upd765_status[2])==0)
214   {
215      return;
216   }
217
218   fdc->upd765_status[0] |= 0x040;
219}
220
221
222static int upd765_n_to_bytes(int n)
223{
224   /* 0-> 128 bytes, 1->256 bytes, 2->512 bytes etc */
225   /* data_size = ((1<<(N+7)) */
226   return 1<<(n+7);
227}
228
229static void upd765_set_data_request(device_t *device)
230{
231   upd765_t *fdc = get_safe_token(device);
232   fdc->FDC_main |= 0x080;
233}
234
235static void upd765_clear_data_request(device_t *device)
236{
237   upd765_t *fdc = get_safe_token(device);
238   fdc->FDC_main &= ~0x080;
239}
240
241static int upd765_get_rdy(device_t *device)
242{
243   upd765_t *fdc = get_safe_token(device);
244
245   if (fdc->intf->rdy_pin == UPD765_RDY_PIN_CONNECTED)
246   {
247      device_t *img = current_image(device);
248      return floppy_drive_get_flag_state(img, FLOPPY_DRIVE_READY);
249   }
250   else
251      return 1;
252}
253
254static void upd765_seek_complete(device_t *device)
255{
256   /* tested on Amstrad CPC */
257
258   /* if a seek is done without drive connected: */
259   /*  abnormal termination of command,
260        seek complete,
261        not ready
262    */
263
264   /* if a seek is done with drive connected, but disc missing: */
265   /* abnormal termination of command,
266        seek complete,
267        not ready */
268
269   /* if a seek is done with drive connected and disc in drive */
270   /* seek complete */
271
272
273   /* On the PC however, it appears that recalibrates and seeks can be performed without
274    a disc in the drive. */
275
276   /* Therefore, the above output is dependant on the state of the drive */
277
278   /* In the Amstrad CPC, the drive select is provided by the UPD765. A single port is also
279    assigned for setting the drive motor state. The motor state controls the motor of the selected
280    drive */
281
282   /* On the PC the drive can be selected with the DIGITAL OUTPUT REGISTER, and the motor of each
283    of the 4 possible drives is also settable using the same register */
284
285   /* Assumption for PC: (NOT TESTED - NEEDS VERIFICATION) */
286
287   /* If a seek is done without drive connected: */
288   /* abnormal termination of command,
289        seek complete,
290        fault
291        */
292
293   /* if a seek is done with drive connected, but disc missing: */
294   /* seek complete */
295
296   /* if a seek is done with drive connected and disc in drive: */
297   /* seek complete */
298
299   /* On Amstrad CPC:
300        If drive not connected, or drive connected but disc not in drive, not ready!
301        If drive connected and drive motor on, ready!
302       On PC:
303        Drive is always ready!
304
305        In 37c78 docs, the ready bits of the upd765 are marked as unused.
306        This indicates it is always ready!!!!!
307    */
308
309   device_t *img = current_image(device);
310   upd765_t *fdc = get_safe_token(device);
311
312   fdc->pcn[fdc->drive] = fdc->ncn;
313
314   fdc->upd765_status[0] = 0x20;
315
316   /* drive ready? */
317   if (img != NULL && upd765_get_rdy(device))
318   {
319      /* recalibrate? */
320      if (fdc->upd765_flags & UPD765_SEEK_OPERATION_IS_RECALIBRATE)
321      {
322         /* not at track 0? */
323         if (fdc->pcn[fdc->drive] != 0)
324            /* no, track 0 failed after 77 steps */
325            fdc->upd765_status[0] |= 0x40 | 0x10;
326      }
327   }
328   else
329   {
330      /* abnormal termination, not ready */
331      fdc->upd765_status[0] |= 0x40 | 0x08;
332   }
333
334   /* set drive and side. note: commented out side to avoid problems with the tf20 */
335   fdc->upd765_status[0] |= fdc->drive; //| (fdc->side<<2);
336
337   upd765_set_int(device,0);
338   upd765_set_int(device,1);
339
340   fdc->upd765_flags &= ~UPD765_SEEK_ACTIVE;
341
342   upd765_idle(device);
343}
344
345static TIMER_CALLBACK(upd765_seek_timer_callback)
346{
347   device_t *device = (device_t *)ptr;
348   upd765_t *fdc = get_safe_token(device);
349   /* seek complete */
350   upd765_seek_complete(device);
351
352   fdc->seek_timer->reset();
353}
354
355static void upd765_timer_func(device_t *device, int timer_type)
356{
357   upd765_t *fdc = get_safe_token(device);
358   /* type 0 = data transfer mode in execution phase */
359   if (fdc->timer_type == 0)
360   {
361      /* set data request */
362      upd765_set_data_request(device);
363
364      fdc->timer_type = 4;
365
366      if (!(fdc->upd765_flags & UPD765_DMA_MODE))
367      {
368         if (fdc->upd765_command_bytes[0] & UPD765_MF)
369         {
370            /* MFM */
371            fdc->timer->reset(attotime::from_usec(13));
372         }
373         else
374         {
375            /* FM */
376            fdc->timer->reset(attotime::from_usec(27));
377         }
378      }
379      else
380      {
381         upd765_timer_func(device, fdc->timer_type);
382      }
383   }
384   else if (fdc->timer_type==2)
385   {
386      /* result phase begin */
387
388      /* generate a int for specific commands */
389      switch (fdc->command) {
390      case 2:      /* read a track */
391      case 5:      /* write data */
392      case 6:      /* read data */
393      case 9:      /* write deleted data */
394      case 10:   /* read id */
395      case 12:   /* read deleted data */
396      case 13:   /* format at track */
397      case 17:   /* scan equal */
398      case 19:   /* scan low or equal */
399      case 29:   /* scan high or equal */
400         upd765_set_int(device,1);
401         break;
402
403      default:
404         break;
405      }
406
407      upd765_set_data_request(device);
408
409      fdc->timer->reset();
410   }
411   else if (fdc->timer_type == 4)
412   {
413      /* if in dma mode, a int is not generated per byte. If not in  DMA mode
414        a int is generated per byte */
415      if (fdc->upd765_flags & UPD765_DMA_MODE)
416      {
417         upd765_set_dma_drq(device,1);
418      }
419      else
420      {
421         if (fdc->FDC_main & (1<<7))
422         {
423            /* set int to indicate data is ready */
424            upd765_set_int(device,1);
425         }
426      }
427
428      fdc->timer->reset();
429   }
430}
431
432static TIMER_CALLBACK(upd765_timer_callback)
433{
434   device_t *device = (device_t *)ptr;
435   upd765_timer_func(device,param);
436}
437
438/* after (32-27) the DRQ is set, then 27 us later, the int is set.
439I don't know if this is correct, but it is required for the PCW driver.
440In this driver, the first NMI calls the handler function, furthur NMI's are
441effectively disabled by reading the data before the NMI int can be set.
442*/
443
444static void upd765_setup_timed_generic(device_t *device, int timer_type, attotime duration)
445{
446   upd765_t *fdc = get_safe_token(device);
447
448   fdc->timer_type = timer_type;
449
450   if (!(fdc->upd765_flags & UPD765_DMA_MODE))
451   {
452      fdc->timer->adjust(duration);
453   }
454   else
455   {
456      upd765_timer_func(device,fdc->timer_type);
457      fdc->timer->reset();
458   }
459}
460
461/* setup data request */
462static void upd765_setup_timed_data_request(device_t *device, int bytes)
463{
464   /* setup timer to trigger in UPD765_DATA_RATE us */
465   upd765_setup_timed_generic(device, 0, attotime::from_usec(32-27)   /*UPD765_DATA_RATE)*bytes*/);
466}
467
468/* setup result data request */
469static void upd765_setup_timed_result_data_request(device_t *device)
470{
471   upd765_setup_timed_generic(device, 2, attotime::from_usec(UPD765_DATA_RATE*2));
472}
473
474
475/* sets up a timer to issue a seek complete in signed_tracks time */
476static void upd765_setup_timed_int(device_t *device,int signed_tracks)
477{
478   upd765_t *fdc = get_safe_token(device);
479   /* setup timer to signal after seek time is complete */
480   fdc->seek_timer->adjust(attotime::from_double(fdc->srt_in_ms*abs(signed_tracks)*0.001));
481}
482
483static void upd765_seek_setup(device_t *device, int is_recalibrate)
484{
485   device_t *img;
486   int signed_tracks;
487   upd765_t *fdc = get_safe_token(device);
488
489   fdc->upd765_flags |= UPD765_SEEK_ACTIVE;
490
491   if (is_recalibrate)
492   {
493      /* head cannot be specified with recalibrate */
494      fdc->upd765_command_bytes[1] &=~0x04;
495   }
496
497   upd765_setup_drive_and_side(device);
498
499   img = current_image(device);
500
501   fdc->FDC_main |= (1<<fdc->drive);
502   fdc->FDC_main |= 0x20;  // execution phase
503   fdc->FDC_main &= ~0x10;  // not busy, can send another seek/recalibrate
504                            // for a different drive, or sense int status
505
506   /* recalibrate command? */
507   if (is_recalibrate)
508   {
509      fdc->upd765_flags |= UPD765_SEEK_OPERATION_IS_RECALIBRATE;
510
511      fdc->ncn = 0;
512
513      /* if drive is already at track 0, or drive is not ready */
514      if (img == NULL || floppy_tk00_r(img) == CLEAR_LINE || (!upd765_get_rdy(device)))
515      {
516         /* seek completed */
517//          upd765_seek_complete(device);
518         // delay for the time of 1 step, the PCW does not like immediate recalibrates
519         upd765_setup_timed_int(device,1);
520      }
521      else
522      {
523         /* is drive present? */
524         if (1) //image_slotexists(img)) //fix me
525         {
526            /* yes - calculate real number of tracks to seek */
527
528            int current_track;
529
530            /* get current track */
531            current_track = floppy_drive_get_current_track(img);
532
533            /* get number of tracks to seek */
534            signed_tracks = -current_track;
535         }
536         else
537         {
538            /* no, seek 77 tracks and then stop */
539            /* true for UPD765A, but not for other variants */
540            signed_tracks = -77;
541         }
542
543         if (signed_tracks!=0)
544         {
545            /* perform seek - if drive isn't present it will not do anything */
546            floppy_drive_seek(img, signed_tracks);
547
548            upd765_setup_timed_int(device,signed_tracks);
549         }
550         else
551         {
552            upd765_seek_complete(device);
553         }
554      }
555   }
556   else
557   {
558
559      fdc->upd765_flags &= ~UPD765_SEEK_OPERATION_IS_RECALIBRATE;
560
561      fdc->ncn = fdc->upd765_command_bytes[2];
562
563      /* get signed tracks */
564      signed_tracks = fdc->ncn - fdc->pcn[fdc->drive];
565
566      /* if no tracks to seek, or drive is not ready, seek is complete */
567      if (img == NULL || (signed_tracks==0) || (!upd765_get_rdy(device)))
568      {
569         upd765_seek_complete(device);
570      }
571      else
572      {
573         /* perform seek - if drive isn't present it will not do anything */
574         floppy_drive_seek(img, signed_tracks);
575
576         /* seek complete - issue an interrupt */
577         upd765_setup_timed_int(device,signed_tracks);
578      }
579   }
580//    upd765_idle(device);
581}
582
583
584
585static void upd765_setup_execution_phase_read(device_t *device, char *ptr, int size)
586{
587   upd765_t *fdc = get_safe_token(device);
588
589   fdc->FDC_main &= ~0x040;                     /* FDC->CPU */
590
591   fdc->upd765_transfer_bytes_count = 0;
592   fdc->upd765_transfer_bytes_remaining = size;
593   fdc->execution_phase_data = ptr;
594   fdc->upd765_phase = UPD765_EXECUTION_PHASE_READ;
595
596   upd765_setup_timed_data_request(device, 1);
597}
598
599static void upd765_setup_execution_phase_write(device_t *device, char *ptr, int size)
600{
601   upd765_t *fdc = get_safe_token(device);
602
603   fdc->FDC_main &= ~0x040;                     /* FDC->CPU */
604
605   fdc->upd765_transfer_bytes_count = 0;
606   fdc->upd765_transfer_bytes_remaining = size;
607   fdc->execution_phase_data = ptr;
608   fdc->upd765_phase = UPD765_EXECUTION_PHASE_WRITE;
609
610   /* setup a data request with first byte */
611   upd765_setup_timed_data_request(device,1);
612}
613
614
615static void upd765_setup_result_phase(device_t *device, int byte_count)
616{
617   upd765_t *fdc = get_safe_token(device);
618
619   fdc->FDC_main |= 0x040;                     /* FDC->CPU */
620   fdc->FDC_main &= ~0x020;                    /* not execution phase */
621
622   fdc->upd765_transfer_bytes_count = 0;
623   fdc->upd765_transfer_bytes_remaining = byte_count;
624   fdc->upd765_phase = UPD765_RESULT_PHASE;
625
626   upd765_setup_timed_result_data_request(device);
627}
628
629void upd765_idle(device_t *device)
630{
631   upd765_t *fdc = get_safe_token(device);
632
633   fdc->FDC_main &= ~0x040;                     /* CPU->FDC */
634   fdc->FDC_main &= ~0x020;                    /* not execution phase */
635   fdc->FDC_main &= ~0x010;                     /* not busy */
636   fdc->upd765_phase = UPD765_COMMAND_PHASE_FIRST_BYTE;
637
638   upd765_set_data_request(device);
639}
640
641
642
643/* change flags */
644static void upd765_change_flags(device_t *device,unsigned int flags, unsigned int mask)
645{
646   unsigned int new_flags;
647   unsigned int changed_flags;
648   upd765_t *fdc = get_safe_token(device);
649
650   assert((flags & ~mask) == 0);
651
652   /* compute the new flags and which ones have changed */
653   new_flags = fdc->upd765_flags & ~mask;
654   new_flags |= flags;
655   changed_flags = fdc->upd765_flags ^ new_flags;
656   fdc->upd765_flags = new_flags;
657
658   /* if interrupt changed, call the handler */
659   if (changed_flags & UPD765_INT)
660      fdc->out_int_func((fdc->upd765_flags & UPD765_INT) ? 1 : 0);
661
662   /* if DRQ changed, call the handler */
663   if (changed_flags & UPD765_DMA_DRQ)
664      fdc->out_drq_func((fdc->upd765_flags & UPD765_DMA_DRQ) ? 1 : 0);
665}
666
667
668
669/* set int output */
670static void upd765_set_int(device_t *device, int state)
671{
672   if (LOG_INTERRUPT)
673      logerror("upd765_set_int(): state=%d\n", state);
674   upd765_change_flags(device, state ? UPD765_INT : 0, UPD765_INT);
675}
676
677
678
679/* set dma request output */
680static void upd765_set_dma_drq(device_t *device, int state)
681{
682   upd765_change_flags(device, state ? UPD765_DMA_DRQ : 0, UPD765_DMA_DRQ);
683}
684
685
686
687/* Drive ready */
688
689/*
690
691A drive will report ready if:
692- drive is selected
693- disc is in the drive
694- disk is rotating at a constant speed (normally 300rpm)
695
696On more modern PCs, a ready signal is not provided by the drive.
697This signal is not used in the PC design and was eliminated to save costs
698If you look at the datasheets for the modern UPD765 variants, you will see the Ready
699signal is not mentioned.
700
701On the original UPD765A, ready signal is required, and some commands will fail if the drive
702is not ready.
703
704
705
706
707*/
708
709
710
711
712/* done when ready state of drive changes */
713/* this ignores if command is active, in which case command should terminate immediatly
714with error */
715static void upd765_set_ready_change_callback(device_t *controller, device_t *img, int state)
716{
717   upd765_t *fdc = get_safe_token(controller);
718   int drive = floppy_get_drive(img);
719
720   if (LOG_EXTRA)
721      logerror("upd765: ready state change\n");
722
723   /* drive that changed state */
724   fdc->upd765_status[0] = 0x0c0 | drive;
725
726   /* not ready */
727   if (state==0 && fdc->intf->rdy_pin == UPD765_RDY_PIN_CONNECTED )
728      fdc->upd765_status[0] |= 8;
729
730   /* trigger an int */
731   upd765_set_int(controller, 1);
732}
733
734
735/* terminal count input */
736WRITE_LINE_DEVICE_HANDLER( upd765_tc_w )
737{
738   int old_state;
739   upd765_t *fdc = get_safe_token(device);
740
741   old_state = fdc->upd765_flags;
742
743   /* clear drq */
744   upd765_set_dma_drq(device, 0);
745
746   fdc->upd765_flags &= ~UPD765_TC;
747   if (state)
748   {
749      fdc->upd765_flags |= UPD765_TC;
750   }
751
752   /* changed state? */
753   if (((fdc->upd765_flags^old_state) & UPD765_TC)!=0)
754   {
755      /* now set? */
756      if ((fdc->upd765_flags & UPD765_TC)!=0)
757      {
758         /* yes */
759         if (fdc->timer)
760         {
761            if (fdc->timer_type==0)
762            {
763               fdc->timer->reset();
764
765
766            }
767         }
768
769#ifdef NO_END_OF_CYLINDER
770         fdc->command_timer->adjust(attotime::zero);
771#else
772         upd765_update_state(device);
773#endif
774      }
775   }
776}
777
778READ8_DEVICE_HANDLER( upd765_status_r )
779{
780   upd765_t *fdc = get_safe_token(device);
781   if (LOG_EXTRA)
782      logerror("%s: upd765_status_r: %02x\n", device->machine().describe_context(), fdc->FDC_main);
783   return fdc->FDC_main;
784}
785
786
787/* control mark handling code */
788
789/* if SK==1, and we are executing a read data command, and a deleted data mark is found,
790skip it.
791if SK==1, and we are executing a read deleted data command, and a data mark is found,
792skip it. */
793
794static int upd765_read_skip_sector(device_t *device)
795{
796   upd765_t *fdc = get_safe_token(device);
797   /* skip set? */
798   if ((fdc->upd765_command_bytes[0] & (1<<5))!=0)
799   {
800      /* read data? */
801      if (fdc->command == 0x06)
802      {
803         /* did we just find a sector with deleted data mark? */
804         if (fdc->data_type == UPD765_DAM_DELETED_DATA)
805         {
806            /* skip it */
807            return TRUE;
808         }
809      }
810      /* deleted data? */
811      else
812      if (fdc->command == 0x0c)
813      {
814         /* did we just find a sector with data mark ? */
815         if (fdc->data_type == UPD765_DAM_DATA)
816         {
817            /* skip it */
818            return TRUE;
819         }
820      }
821   }
822
823   /* do not skip */
824   return FALSE;
825}
826
827/* this is much closer to how the upd765 actually gets sectors */
828/* used by read data, read deleted data, write data, write deleted data */
829/* What the upd765 does:
830
831  - get next sector id from disc
832  - if sector id matches id specified in command, it will
833    search for next data block and read data from it.
834
835  - if the index is seen twice while it is searching for a sector, then the sector cannot be found
836*/
837
838static void upd765_get_next_id(device_t *device, chrn_id *id)
839{
840   upd765_t *fdc = get_safe_token(device);
841   device_t *img = current_image(device);
842
843   /* get next id from disc */
844   floppy_drive_get_next_id(img, fdc->side,id);
845
846   fdc->sector_id = id->data_id;
847
848   /* set correct data type */
849   fdc->data_type = UPD765_DAM_DATA;
850   if (id->flags & ID_FLAG_DELETED_DATA)
851   {
852      fdc->data_type = UPD765_DAM_DELETED_DATA;
853   }
854}
855
856static int upd765_get_matching_sector(device_t *device)
857{
858   upd765_t *fdc = get_safe_token(device);
859   device_t *img = current_image(device);
860   chrn_id id;
861
862   /* number of times we have seen index hole */
863   int index_count = 0;
864
865   /* get sector id's */
866   do
867    {
868      upd765_get_next_id(device, &id);
869
870      /* tested on Amstrad CPC - All bytes must match, otherwise
871        a NO DATA error is reported */
872      if (id.R == fdc->upd765_command_bytes[4])
873      {
874         if (id.C == fdc->upd765_command_bytes[2])
875         {
876            if (id.H == fdc->upd765_command_bytes[3])
877            {
878               if (id.N == fdc->upd765_command_bytes[5])
879               {
880                  /* end of cylinder is set if:
881                        1. sector data is read completely (i.e. no other errors occur like
882                        no data.
883                        2. sector being read is same specified by EOT
884                        3. terminal count is not received */
885                  if (fdc->upd765_command_bytes[4]==fdc->upd765_command_bytes[6])
886                  {
887                     /* set end of cylinder */
888                     fdc->upd765_status[1] |= UPD765_ST1_END_OF_CYLINDER;
889                  }
890
891                  return TRUE;
892               }
893            }
894         }
895         else
896         {
897            /* the specified sector ID was found, however, the C value specified
898                in the read/write command did not match the C value read from the disc */
899
900            /* no data - checked on Amstrad CPC */
901            fdc->upd765_status[1] |= UPD765_ST1_NO_DATA;
902            /* bad C value */
903            fdc->upd765_status[2] |= UPD765_ST2_WRONG_CYLINDER;
904
905            if (id.C == 0x0ff)
906            {
907               /* the C value is 0x0ff which indicates a bad track in the IBM soft-sectored
908                    format */
909               fdc->upd765_status[2] |= UPD765_ST2_BAD_CYLINDER;
910            }
911
912            return FALSE;
913         }
914      }
915
916       /* index set? */
917      if (floppy_drive_get_flag_state(img, FLOPPY_DRIVE_INDEX))
918      {
919         index_count++;
920      }
921
922   }
923   while (index_count!=2);
924
925   if (fdc->upd765_command_bytes[4] != fdc->upd765_command_bytes[6])
926   {
927      /* no data - specified sector ID was not found */
928      fdc->upd765_status[1] |= UPD765_ST1_NO_DATA;
929   }
930
931   return 0;
932}
933
934static void upd765_read_complete(device_t *device)
935{
936   upd765_t *fdc = get_safe_token(device);
937/* causes problems!!! - need to fix */
938#ifdef NO_END_OF_CYLINDER
939   /* set end of cylinder */
940   fdc->upd765_status[1] &= ~UPD765_ST1_END_OF_CYLINDER;
941#else
942   /* completed read command */
943
944   /* end of cylinder is set when:
945        - a whole sector has been read
946        - terminal count input is not set
947        - AND the the sector specified by EOT was read
948        */
949
950   /* if end of cylinder is set, and we did receive a terminal count, then clear it */
951   if ((fdc->upd765_flags & UPD765_TC)!=0)
952   {
953      /* set end of cylinder */
954      fdc->upd765_status[1] &= ~UPD765_ST1_END_OF_CYLINDER;
955   }
956#endif
957
958   upd765_setup_st0(device);
959
960   fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
961   fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
962   fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
963   fdc->upd765_result_bytes[3] = fdc->upd765_command_bytes[2]; /* C */
964   fdc->upd765_result_bytes[4] = fdc->upd765_command_bytes[3]; /* H */
965   fdc->upd765_result_bytes[5] = fdc->upd765_command_bytes[4]; /* R */
966   fdc->upd765_result_bytes[6] = fdc->upd765_command_bytes[5]; /* N */
967
968   upd765_setup_result_phase(device,7);
969}
970
971static void upd765_read_data(device_t *device)
972{
973   upd765_t *fdc = get_safe_token(device);
974   device_t *img = current_image(device);
975
976   if (!upd765_get_rdy(device))
977   {
978      fdc->upd765_status[0] = 0x0c0 | (1<<4) | fdc->drive | (fdc->side<<2);
979      fdc->upd765_status[1] = 0x00;
980      fdc->upd765_status[2] = 0x00;
981
982      fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
983      fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
984      fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
985      fdc->upd765_result_bytes[3] = fdc->upd765_command_bytes[2]; /* C */
986      fdc->upd765_result_bytes[4] = fdc->upd765_command_bytes[3]; /* H */
987      fdc->upd765_result_bytes[5] = fdc->upd765_command_bytes[4]; /* R */
988      fdc->upd765_result_bytes[6] = fdc->upd765_command_bytes[5]; /* N */
989      upd765_setup_result_phase(device,7);
990      return;
991   }
992
993   if (LOG_VERBOSE)
994      logerror("sector c: %02x h: %02x r: %02x n: %02x\n",fdc->upd765_command_bytes[2], fdc->upd765_command_bytes[3],fdc->upd765_command_bytes[4], fdc->upd765_command_bytes[5]);
995
996   /* find a sector to read data from */
997   {
998      int found_sector_to_read;
999
1000      found_sector_to_read = 0;
1001      /* check for finished reading sectors */
1002      do
1003      {
1004         /* get matching sector */
1005         if (upd765_get_matching_sector(device))
1006         {
1007
1008            /* skip it? */
1009            if (upd765_read_skip_sector(device))
1010            {
1011               /* yes */
1012
1013               /* check that we haven't finished reading all sectors */
1014               if (upd765_sector_count_complete(device))
1015               {
1016                  /* read complete */
1017                  upd765_read_complete(device);
1018                  return;
1019               }
1020
1021               /* read not finished */
1022
1023               /* increment sector count */
1024               upd765_increment_sector(device);
1025            }
1026            else
1027            {
1028               /* found a sector to read */
1029               found_sector_to_read = 1;
1030            }
1031         }
1032         else
1033         {
1034            /* error in finding sector */
1035            upd765_read_complete(device);
1036            return;
1037         }
1038      }
1039      while (found_sector_to_read==0);
1040   }
1041
1042   {
1043      int data_size;
1044
1045      data_size = upd765_n_to_bytes(fdc->upd765_command_bytes[5]);
1046
1047      floppy_drive_read_sector_data(img, fdc->side, fdc->sector_id,fdc->data_buffer,data_size);
1048
1049        upd765_setup_execution_phase_read(device,fdc->data_buffer, data_size);
1050   }
1051}
1052
1053
1054static void upd765_format_track(device_t *device)
1055{
1056   upd765_t *fdc = get_safe_token(device);
1057   device_t *img = current_image(device);
1058
1059   /* write protected? */
1060   if (floppy_wpt_r(img) == CLEAR_LINE)
1061   {
1062      fdc->upd765_status[1] |= UPD765_ST1_NOT_WRITEABLE;
1063
1064      upd765_setup_st0(device);
1065      /* TODO: Check result is correct */
1066         fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1067            fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1068            fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1069         fdc->upd765_result_bytes[3] = fdc->format_data[0];
1070         fdc->upd765_result_bytes[4] = fdc->format_data[1];
1071         fdc->upd765_result_bytes[5] = fdc->format_data[2];
1072         fdc->upd765_result_bytes[6] = fdc->format_data[3];
1073         upd765_setup_result_phase(device,7);
1074
1075      return;
1076   }
1077
1078    upd765_setup_execution_phase_write(device, &fdc->format_data[0], 4);
1079}
1080
1081static void upd765_read_a_track(device_t *device)
1082{
1083   upd765_t *fdc = get_safe_token(device);
1084   int data_size;
1085
1086   /* SKIP not allowed with this command! */
1087
1088   /* get next id */
1089   chrn_id id;
1090
1091   upd765_get_next_id(device, &id);
1092
1093   /* TO BE CONFIRMED! */
1094   /* check id from disc */
1095   if (id.C==fdc->upd765_command_bytes[2])
1096   {
1097      if (id.H==fdc->upd765_command_bytes[3])
1098      {
1099         if (id.R==fdc->upd765_command_bytes[4])
1100         {
1101            if (id.N==fdc->upd765_command_bytes[5])
1102            {
1103               /* if ID found, then no data is not set */
1104               /* otherwise no data will remain set */
1105               fdc->upd765_status[1] &=~UPD765_ST1_NO_DATA;
1106            }
1107         }
1108      }
1109   }
1110
1111
1112   data_size = upd765_n_to_bytes(id.N);
1113
1114   floppy_drive_read_sector_data(current_image(device), fdc->side, fdc->sector_id,fdc->data_buffer,data_size);
1115
1116   upd765_setup_execution_phase_read(device,fdc->data_buffer, data_size);
1117}
1118
1119static int upd765_just_read_last_sector_on_track(device_t *device)
1120{
1121   if (floppy_drive_get_flag_state(current_image(device), FLOPPY_DRIVE_INDEX))
1122      return 1;
1123   return 0;
1124}
1125
1126static void upd765_write_complete(device_t *device)
1127{
1128   upd765_t *fdc = get_safe_token(device);
1129
1130/* causes problems!!! - need to fix */
1131#ifdef NO_END_OF_CYLINDER
1132        /* set end of cylinder */
1133        fdc->upd765_status[1] &= ~UPD765_ST1_END_OF_CYLINDER;
1134#else
1135   /* completed read command */
1136
1137   /* end of cylinder is set when:
1138     - a whole sector has been read
1139     - terminal count input is not set
1140     - AND the the sector specified by EOT was read
1141     */
1142
1143   /* if end of cylinder is set, and we did receive a terminal count, then clear it */
1144   if ((fdc->upd765_flags & UPD765_TC)!=0)
1145   {
1146      /* set end of cylinder */
1147      fdc->upd765_status[1] &= ~UPD765_ST1_END_OF_CYLINDER;
1148   }
1149#endif
1150
1151   upd765_setup_st0(device);
1152
1153    fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1154    fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1155    fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1156    fdc->upd765_result_bytes[3] = fdc->upd765_command_bytes[2]; /* C */
1157    fdc->upd765_result_bytes[4] = fdc->upd765_command_bytes[3]; /* H */
1158    fdc->upd765_result_bytes[5] = fdc->upd765_command_bytes[4]; /* R */
1159    fdc->upd765_result_bytes[6] = fdc->upd765_command_bytes[5]; /* N */
1160
1161    upd765_setup_result_phase(device,7);
1162}
1163
1164
1165static void upd765_write_data(device_t *device)
1166{
1167   upd765_t *fdc = get_safe_token(device);
1168   if (!upd765_get_rdy(device))
1169   {
1170      fdc->upd765_status[0] = 0x0c0 | (1<<4) | fdc->drive | (fdc->side<<2);
1171        fdc->upd765_status[1] = 0x00;
1172        fdc->upd765_status[2] = 0x00;
1173
1174        fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1175        fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1176        fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1177        fdc->upd765_result_bytes[3] = fdc->upd765_command_bytes[2]; /* C */
1178        fdc->upd765_result_bytes[4] = fdc->upd765_command_bytes[3]; /* H */
1179        fdc->upd765_result_bytes[5] = fdc->upd765_command_bytes[4]; /* R */
1180        fdc->upd765_result_bytes[6] = fdc->upd765_command_bytes[5]; /* N */
1181      upd765_setup_result_phase(device,7);
1182      return;
1183   }
1184
1185   /* write protected? */
1186   if (floppy_wpt_r(current_image(device)) == CLEAR_LINE)
1187   {
1188      fdc->upd765_status[1] |= UPD765_ST1_NOT_WRITEABLE;
1189
1190      upd765_write_complete(device);
1191      return;
1192   }
1193
1194   if (upd765_get_matching_sector(device))
1195   {
1196      int data_size;
1197
1198      data_size = upd765_n_to_bytes(fdc->upd765_command_bytes[5]);
1199
1200        upd765_setup_execution_phase_write(device,fdc->data_buffer, data_size);
1201   }
1202    else
1203    {
1204        upd765_setup_result_phase(device,7);
1205    }
1206}
1207
1208
1209/* return true if we have read all sectors, false if not */
1210static int upd765_sector_count_complete(device_t *device)
1211{
1212   upd765_t *fdc = get_safe_token(device);
1213/* this is not correct?? */
1214#if 1
1215   /* if terminal count has been set - yes */
1216   if (fdc->upd765_flags & UPD765_TC)
1217   {
1218      /* completed */
1219      return 1;
1220   }
1221
1222
1223
1224   /* multi-track? */
1225   if (fdc->upd765_command_bytes[0] & 0x080)
1226   {
1227      /* it appears that in multi-track mode,
1228        the EOT parameter of the command is ignored!? -
1229        or is it ignored the first time and not the next, so that
1230        if it is started on side 0, it will end at EOT on side 1,
1231        but if started on side 1 it will end at end of track????
1232
1233        PC driver requires this to end at last sector on side 1, and
1234        ignore EOT parameter.
1235
1236        To be checked!!!!
1237        */
1238
1239      /* if just read last sector and on side 1 - finish */
1240      if (upd765_just_read_last_sector_on_track(device))
1241      {
1242         if (floppy_get_heads_per_disk(flopimg_get_image(current_image(device)))==1) {
1243            return 1;
1244         } else {
1245            if (fdc->side==1)
1246               return 1;
1247         }
1248      }
1249
1250      /* if not on second side then we haven't finished yet */
1251      if (fdc->side!=1)
1252      {
1253         /* haven't finished yet */
1254         return 0;
1255      }
1256   }
1257   else
1258   {
1259      /* sector id == EOT? */
1260      if ((fdc->upd765_command_bytes[4]==fdc->upd765_command_bytes[6]))
1261      {
1262
1263         /* completed */
1264         return 1;
1265      }
1266   }
1267#else
1268
1269   /* if terminal count has been set - yes */
1270   if (fdc->upd765_flags & UPD765_TC)
1271   {
1272      /* completed */
1273      return 1;
1274   }
1275
1276   /* Multi-Track operation:
1277
1278    Verified on Amstrad CPC.
1279
1280        disc format used:
1281            9 sectors per track
1282            2 sides
1283            Sector IDs: &01, &02, &03, &04, &05, &06, &07, &08, &09
1284
1285        Command specified:
1286            SIDE = 0,
1287            C = 0,H = 0,R = 1, N = 2, EOT = 1
1288        Sectors read:
1289            Sector 1 side 0
1290            Sector 1 side 1
1291
1292        Command specified:
1293            SIDE = 0,
1294            C = 0,H = 0,R = 1, N = 2, EOT = 3
1295        Sectors read:
1296            Sector 1 side 0
1297            Sector 2 side 0
1298            Sector 3 side 0
1299            Sector 1 side 1
1300            Sector 2 side 1
1301            Sector 3 side 1
1302
1303
1304        Command specified:
1305            SIDE = 0,
1306            C = 0, H = 0, R = 7, N = 2, EOT = 3
1307        Sectors read:
1308            Sector 7 side 0
1309            Sector 8 side 0
1310            Sector 9 side 0
1311            Sector 10 not found. Error "No Data"
1312
1313        Command specified:
1314            SIDE = 1,
1315            C = 0, H = 1, R = 1, N = 2, EOT = 1
1316        Sectors read:
1317            Sector 1 side 1
1318
1319        Command specified:
1320            SIDE = 1,
1321            C = 0, H = 1, R = 1, N = 2, EOT = 2
1322        Sectors read:
1323            Sector 1 side 1
1324            Sector 1 side 2
1325
1326  */
1327
1328   /* sector id == EOT? */
1329   if ((fdc->upd765_command_bytes[4]==fdc->upd765_command_bytes[6]))
1330   {
1331      /* multi-track? */
1332      if (fdc->upd765_command_bytes[0] & 0x080)
1333      {
1334         /* if we have reached EOT (fdc->upd765_command_bytes[6])
1335            on side 1, then read is complete */
1336         if (fdc->side==1)
1337            return 1;
1338
1339         return 0;
1340
1341      }
1342
1343      /* completed */
1344      return 1;
1345   }
1346#endif
1347   /* not complete */
1348   return 0;
1349}
1350
1351static void   upd765_increment_sector(device_t *device)
1352{
1353   upd765_t *fdc = get_safe_token(device);
1354   /* multi-track? */
1355   if (fdc->upd765_command_bytes[0] & 0x080)
1356   {
1357      /* reached EOT? */
1358      /* if (fdc->upd765_command_bytes[4]==fdc->upd765_command_bytes[6])*/
1359      if (upd765_just_read_last_sector_on_track(device))
1360      {
1361         /* yes */
1362
1363         /* reached EOT */
1364         /* change side to 1 */
1365         fdc->side = 1;
1366         /* reset sector id to 1 */
1367         fdc->upd765_command_bytes[4] = 1;
1368         /* set head to 1 for get next sector test */
1369         fdc->upd765_command_bytes[3] = 1;
1370      }
1371      else
1372      {
1373         /* increment */
1374         fdc->upd765_command_bytes[4]++;
1375      }
1376
1377   }
1378   else
1379   {
1380      fdc->upd765_command_bytes[4]++;
1381   }
1382}
1383
1384/* control mark handling code */
1385
1386/* if SK==0, and we are executing a read data command, and a deleted data sector is found,
1387the data is not skipped. The data is read, but the control mark is set and the read is stopped */
1388/* if SK==0, and we are executing a read deleted data command, and a data sector is found,
1389the data is not skipped. The data is read, but the control mark is set and the read is stopped */
1390static int upd765_read_data_stop(device_t *device)
1391{
1392   upd765_t *fdc = get_safe_token(device);
1393   /* skip not set? */
1394   if ((fdc->upd765_command_bytes[0] & (1<<5))==0)
1395   {
1396      /* read data? */
1397      if (fdc->command == 0x06)
1398      {
1399         /* did we just read a sector with deleted data? */
1400         if (fdc->data_type == UPD765_DAM_DELETED_DATA)
1401         {
1402            /* set control mark */
1403            fdc->upd765_status[2] |= UPD765_ST2_CONTROL_MARK;
1404
1405            /* quit */
1406            return TRUE;
1407         }
1408      }
1409      /* deleted data? */
1410      else
1411      if (fdc->command == 0x0c)
1412      {
1413         /* did we just read a sector with data? */
1414         if (fdc->data_type == UPD765_DAM_DATA)
1415         {
1416            /* set control mark */
1417            fdc->upd765_status[2] |= UPD765_ST2_CONTROL_MARK;
1418
1419            /* quit */
1420            return TRUE;
1421         }
1422      }
1423   }
1424
1425   /* continue */
1426   return FALSE;
1427}
1428
1429static TIMER_CALLBACK(upd765_continue_command)
1430{
1431   device_t *device = (device_t *)ptr;
1432   upd765_t *fdc = get_safe_token(device);
1433   if ((fdc->upd765_phase == UPD765_EXECUTION_PHASE_READ) ||
1434      (fdc->upd765_phase == UPD765_EXECUTION_PHASE_WRITE))
1435   {
1436      switch (fdc->command)
1437        {
1438         /* read a track */
1439         case 0x02:
1440         {
1441            fdc->sector_counter++;
1442
1443            /* sector counter == EOT */
1444            if (fdc->sector_counter==fdc->upd765_command_bytes[6])
1445            {
1446               /* TODO: Add correct info here */
1447
1448               fdc->upd765_status[1] |= UPD765_ST1_END_OF_CYLINDER;
1449
1450               upd765_setup_st0(device);
1451
1452               fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1453               fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1454               fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1455               fdc->upd765_result_bytes[3] = fdc->upd765_command_bytes[2]; /* C */
1456               fdc->upd765_result_bytes[4] = fdc->upd765_command_bytes[3]; /* H */
1457               fdc->upd765_result_bytes[5] = fdc->upd765_command_bytes[4]; /* R */
1458               fdc->upd765_result_bytes[6] = fdc->upd765_command_bytes[5]; /* N */
1459
1460               upd765_setup_result_phase(device,7);
1461            }
1462            else
1463            {
1464               upd765_read_a_track(device);
1465            }
1466         }
1467         break;
1468
1469         /* format track */
1470         case 0x0d:
1471         {
1472            floppy_drive_format_sector(current_image(device), fdc->side, fdc->sector_counter,
1473               fdc->format_data[0], fdc->format_data[1],
1474               fdc->format_data[2], fdc->format_data[3],
1475               fdc->upd765_command_bytes[5]);
1476
1477            fdc->sector_counter++;
1478
1479            /* sector_counter = SC */
1480            if (fdc->sector_counter == fdc->upd765_command_bytes[3])
1481            {
1482               /* TODO: Check result is correct */
1483               fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1484               fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1485               fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1486               fdc->upd765_result_bytes[3] = fdc->format_data[0];
1487               fdc->upd765_result_bytes[4] = fdc->format_data[1];
1488               fdc->upd765_result_bytes[5] = fdc->format_data[2];
1489               fdc->upd765_result_bytes[6] = fdc->format_data[3];
1490               upd765_setup_result_phase(device,7);
1491            }
1492            else
1493            {
1494               upd765_format_track(device);
1495            }
1496         }
1497         break;
1498
1499         /* write data, write deleted data */
1500         case 0x09:
1501         case 0x05:
1502         {
1503            /* sector id == EOT */
1504            UINT8 ddam;
1505
1506            ddam = 0;
1507            if (fdc->command == 0x09)
1508            {
1509               ddam = 1;
1510            }
1511
1512            /* write data to disc */
1513            floppy_drive_write_sector_data(current_image(device), fdc->side, fdc->sector_id,fdc->data_buffer,upd765_n_to_bytes(fdc->upd765_command_bytes[5]),ddam);
1514
1515            if (upd765_sector_count_complete(device))
1516            {
1517               upd765_increment_sector(device);
1518               upd765_write_complete(device);
1519            }
1520            else
1521            {
1522               upd765_increment_sector(device);
1523               upd765_write_data(device);
1524            }
1525         }
1526         break;
1527
1528         /* read data, read deleted data */
1529         case 0x0c:
1530         case 0x06:
1531         {
1532
1533            /* read all sectors? */
1534
1535            /* sector id == EOT */
1536            if (upd765_sector_count_complete(device) || upd765_read_data_stop(device))
1537            {
1538               upd765_read_complete(device);
1539            }
1540            else
1541            {
1542               upd765_increment_sector(device);
1543               upd765_read_data(device);
1544            }
1545         }
1546         break;
1547
1548         default:
1549            break;
1550      }
1551   }
1552}
1553
1554
1555static int upd765_get_command_byte_count(device_t *device)
1556{
1557   upd765_t *fdc = get_safe_token(device);
1558   fdc->command = fdc->upd765_command_bytes[0] & 0x01f;
1559
1560   if (fdc->version==TYPE_UPD765A)
1561   {
1562       return upd765_cmd_size[fdc->command];
1563    }
1564   else
1565   {
1566      if (fdc->version==TYPE_UPD72065)
1567      {
1568         switch(fdc->upd765_command_bytes[0])
1569         {
1570            case 0x34: // Reset Standby
1571            case 0x35: // Set Standby
1572            case 0x36: // Software Reset
1573               return 1;
1574            default:
1575               return upd765_cmd_size[fdc->command];
1576         }
1577      }
1578      if (fdc->version==TYPE_SMC37C78)
1579      {
1580         switch (fdc->command)
1581         {
1582            /* version */
1583            case 0x010:
1584               return 1;
1585
1586            /* verify */
1587            case 0x016:
1588               return 9;
1589
1590            /* configure */
1591            case 0x013:
1592               return 4;
1593
1594            /* dumpreg */
1595            case 0x0e:
1596               return 1;
1597
1598            /* perpendicular mode */
1599            case 0x012:
1600               return 1;
1601
1602            /* lock */
1603            case 0x014:
1604               return 1;
1605
1606            /* seek/relative seek are together! */
1607
1608            default:
1609               return upd765_cmd_size[fdc->command];
1610         }
1611      }
1612   }
1613
1614   return upd765_cmd_size[fdc->command];
1615}
1616
1617
1618
1619
1620
1621void upd765_update_state(device_t *device)
1622{
1623   upd765_t *fdc = get_safe_token(device);
1624   switch (fdc->upd765_phase) {
1625   case UPD765_RESULT_PHASE:
1626      /* set data reg */
1627      fdc->upd765_data_reg = fdc->upd765_result_bytes[fdc->upd765_transfer_bytes_count];
1628
1629      if (fdc->upd765_transfer_bytes_count == 0)
1630      {
1631         /* clear int for specific commands */
1632         switch (fdc->command) {
1633         case 2:   /* read a track */
1634         case 5:   /* write data */
1635         case 6:   /* read data */
1636         case 9:   /* write deleted data */
1637         case 10:   /* read id */
1638         case 12:   /* read deleted data */
1639         case 13:   /* format at track */
1640         case 17:   /* scan equal */
1641         case 19:   /* scan low or equal */
1642         case 29:   /* scan high or equal */
1643            upd765_set_int(device, 0);
1644            break;
1645
1646         default:
1647            break;
1648         }
1649      }
1650
1651      if (LOG_VERBOSE)
1652         logerror("UPD765: RESULT: %02x\n", fdc->upd765_data_reg);
1653
1654      fdc->upd765_transfer_bytes_count++;
1655      fdc->upd765_transfer_bytes_remaining--;
1656
1657      if (fdc->upd765_transfer_bytes_remaining==0)
1658      {
1659         upd765_idle(device);
1660      }
1661      else
1662      {
1663         upd765_set_data_request(device);
1664      }
1665      break;
1666
1667   case UPD765_EXECUTION_PHASE_READ:
1668      /* setup data register */
1669      fdc->upd765_data_reg = fdc->execution_phase_data[fdc->upd765_transfer_bytes_count];
1670      fdc->upd765_transfer_bytes_count++;
1671      fdc->upd765_transfer_bytes_remaining--;
1672
1673      if (LOG_EXTRA)
1674         logerror("EXECUTION PHASE READ: %02x\n", fdc->upd765_data_reg);
1675
1676      if ((fdc->upd765_transfer_bytes_remaining==0) || (fdc->upd765_flags & UPD765_TC))
1677      {
1678         fdc->command_timer->adjust(attotime::zero);
1679      }
1680      else
1681      {
1682         /* trigger int */
1683         upd765_setup_timed_data_request(device,1);
1684      }
1685      break;
1686
1687   case UPD765_COMMAND_PHASE_FIRST_BYTE:
1688      fdc->FDC_main |= 0x10;                      /* set BUSY */
1689
1690      if (LOG_VERBOSE)
1691         logerror("%s: upd765(): command=0x%02x\n", device->machine().describe_context(), fdc->upd765_data_reg);
1692
1693      /* seek in progress? */
1694      if (fdc->upd765_flags & UPD765_SEEK_ACTIVE)
1695      {
1696         /* any command results in a invalid - I think that seek, recalibrate and
1697            sense interrupt status may work*/
1698         if (fdc->upd765_data_reg != 8)   /* Sense Interrupt Status */
1699            fdc->upd765_data_reg = 0;
1700      }
1701
1702      fdc->upd765_command_bytes[0] = fdc->upd765_data_reg;
1703
1704      fdc->upd765_transfer_bytes_remaining = upd765_get_command_byte_count(device);
1705
1706      fdc->upd765_transfer_bytes_count = 1;
1707      fdc->upd765_transfer_bytes_remaining--;
1708
1709      if (fdc->upd765_transfer_bytes_remaining==0)
1710      {
1711         upd765_setup_command(device);
1712      }
1713      else
1714      {
1715         /* request more data */
1716         upd765_set_data_request(device);
1717         fdc->upd765_phase = UPD765_COMMAND_PHASE_BYTES;
1718      }
1719        break;
1720
1721    case UPD765_COMMAND_PHASE_BYTES:
1722      if (LOG_VERBOSE)
1723         logerror("%s: upd765(): command=0x%02x\n", device->machine().describe_context(), fdc->upd765_data_reg);
1724
1725      fdc->upd765_command_bytes[fdc->upd765_transfer_bytes_count] = fdc->upd765_data_reg;
1726      fdc->upd765_transfer_bytes_count++;
1727      fdc->upd765_transfer_bytes_remaining--;
1728
1729      if (fdc->upd765_transfer_bytes_remaining==0)
1730      {
1731         upd765_setup_command(device);
1732      }
1733      else
1734      {
1735         /* request more data */
1736         upd765_set_data_request(device);
1737      }
1738      break;
1739
1740    case UPD765_EXECUTION_PHASE_WRITE:
1741      fdc->execution_phase_data[fdc->upd765_transfer_bytes_count]=fdc->upd765_data_reg;
1742      fdc->upd765_transfer_bytes_count++;
1743      fdc->upd765_transfer_bytes_remaining--;
1744
1745      if ((fdc->upd765_transfer_bytes_remaining == 0) || (fdc->upd765_flags & UPD765_TC))
1746      {
1747         fdc->command_timer->adjust(attotime::zero);
1748      }
1749      else
1750      {
1751         upd765_setup_timed_data_request(device,1);
1752      }
1753      break;
1754   }
1755}
1756
1757
1758READ8_DEVICE_HANDLER(upd765_data_r)
1759{
1760   upd765_t *fdc = get_safe_token(device);
1761
1762//  if ((fdc->FDC_main & 0x0c0) == 0x0c0)
1763   if ((fdc->FDC_main & 0x080) == 0x080)
1764   {
1765      if (
1766         (fdc->upd765_phase == UPD765_EXECUTION_PHASE_READ) ||
1767         (fdc->upd765_phase == UPD765_EXECUTION_PHASE_WRITE))
1768      {
1769
1770         /* reading the data byte clears the interrupt */
1771         upd765_set_int(device,CLEAR_LINE);
1772      }
1773
1774      /* reset data request */
1775      upd765_clear_data_request(device);
1776
1777      /* update state */
1778      upd765_update_state(device);
1779   }
1780
1781   if (LOG_EXTRA)
1782      logerror("DATA R: %02x\n", fdc->upd765_data_reg);
1783
1784   return fdc->upd765_data_reg;
1785}
1786
1787WRITE8_DEVICE_HANDLER(upd765_data_w)
1788{
1789   upd765_t *fdc = get_safe_token(device);
1790
1791   if (LOG_EXTRA)
1792      logerror("DATA W: %02x\n", data);
1793
1794   /* write data to data reg */
1795   fdc->upd765_data_reg = data;
1796
1797   if ((fdc->FDC_main & 0x0c0)==0x080)
1798   {
1799      if (
1800         (fdc->upd765_phase == UPD765_EXECUTION_PHASE_READ) ||
1801         (fdc->upd765_phase == UPD765_EXECUTION_PHASE_WRITE))
1802      {
1803
1804         /* reading the data byte clears the interrupt */
1805         upd765_set_int(device, CLEAR_LINE);
1806      }
1807
1808      /* reset data request */
1809      upd765_clear_data_request(device);
1810
1811      /* update state */
1812      upd765_update_state(device);
1813   }
1814}
1815
1816static void upd765_setup_invalid(device_t *device)
1817{
1818   upd765_t *fdc = get_safe_token(device);
1819
1820   fdc->command = 0;
1821   fdc->upd765_result_bytes[0] = 0x080;
1822   upd765_setup_result_phase(device,1);
1823}
1824
1825static void upd765_setup_command(device_t *device)
1826{
1827   upd765_t *fdc = get_safe_token(device);
1828
1829   static const char *const commands[] =
1830   {
1831      NULL,                  /* [00] */
1832      NULL,                  /* [01] */
1833      "Read Track",            /* [02] */
1834      "Specify",               /* [03] */
1835      "Sense Drive Status",      /* [04] */
1836      "Write Data",            /* [05] */
1837      "Read Data",            /* [06] */
1838      "Recalibrate",            /* [07] */
1839      "Sense Interrupt Status",   /* [08] */
1840      "Write Deleted Data",      /* [09] */
1841      "Read ID",               /* [0A] */
1842      NULL,                  /* [0B] */
1843      "Read Deleted Data",      /* [0C] */
1844      "Format Track",            /* [0D] */
1845      "Dump Registers",         /* [0E] */
1846      "Seek",                  /* [0F] */
1847      "Version",               /* [10] */
1848      NULL,                  /* [11] */
1849      "Perpendicular Mode",      /* [12] */
1850      "Configure",            /* [13] */
1851      "Lock"                  /* [14] */
1852   };
1853
1854   device_t *img;
1855   const char *cmd = NULL;
1856   chrn_id id;
1857
1858   /* if not in dma mode set execution phase bit */
1859   if (!(fdc->upd765_flags & UPD765_DMA_MODE))
1860   {
1861        fdc->FDC_main |= 0x020;              /* execution phase */
1862   }
1863
1864   if (LOG_COMMAND)
1865   {
1866      if ((fdc->upd765_command_bytes[0] & 0x1f) < ARRAY_LENGTH(commands))
1867         cmd = commands[fdc->upd765_command_bytes[0] & 0x1f];
1868      logerror("upd765_setup_command(): Setting up command 0x%02X (%s)\n",
1869         fdc->upd765_command_bytes[0] & 0x1f, cmd ? cmd : "???");
1870   }
1871
1872   switch (fdc->upd765_command_bytes[0] & 0x1f)
1873   {
1874      case 0x03:      /* specify */
1875         /* setup step rate */
1876         fdc->srt_in_ms = 16-((fdc->upd765_command_bytes[1]>>4) & 0x0f);
1877
1878         fdc->upd765_flags &= ~UPD765_DMA_MODE;
1879
1880         if ((fdc->upd765_command_bytes[2] & 0x01)==0)
1881         {
1882            fdc->upd765_flags |= UPD765_DMA_MODE;
1883         }
1884
1885         upd765_idle(device);
1886         break;
1887
1888      case 0x04:  /* sense drive status */
1889         upd765_setup_drive_and_side(device);
1890         img = current_image(device);
1891
1892         fdc->upd765_status[3] = fdc->drive | (fdc->side<<2);
1893
1894         if (img)
1895         {
1896            fdc->upd765_status[3] |= !floppy_tk00_r(img) << 4;
1897            fdc->upd765_status[3] |= !floppy_wpt_r(img) << 6;
1898
1899            if (upd765_get_rdy(device))
1900            {
1901               fdc->upd765_status[3] |= 0x20;
1902            }
1903         }
1904
1905         fdc->upd765_status[3] |= 0x08;
1906
1907         /* two side and fault not set but should be? */
1908         fdc->upd765_result_bytes[0] = fdc->upd765_status[3];
1909         upd765_setup_result_phase(device,1);
1910         break;
1911
1912      case 0x07:          /* recalibrate */
1913         upd765_seek_setup(device, 1);
1914         break;
1915
1916      case 0x0f:          /* seek */
1917         upd765_seek_setup(device, 0);
1918         break;
1919
1920      case 0x0a:      /* read id */
1921         upd765_setup_drive_and_side(device);
1922         img = current_image(device);
1923
1924         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
1925         fdc->upd765_status[1] = 0;
1926         fdc->upd765_status[2] = 0;
1927
1928         /* drive ready? */
1929         if (upd765_get_rdy(device))
1930         {
1931            /* is disk inserted? */
1932            device_image_interface *image = dynamic_cast<device_image_interface *>( img);
1933            if (image->exists())
1934            {
1935               int index_count = 0;
1936
1937               /* floppy drive is ready and disc is inserted */
1938
1939               /* this is the id that appears when a disc is not formatted */
1940               /* to be checked on Amstrad */
1941               id.C = 0;
1942               id.H = 0;
1943               id.R = 0x01;
1944               id.N = 0x02;
1945
1946               /* repeat for two index counts before quitting */
1947               do
1948               {
1949                  /* get next id from disc */
1950                  if (floppy_drive_get_next_id(img, fdc->side, &id))
1951                  {
1952                     /* got an id - quit */
1953                     break;
1954                  }
1955
1956                  if (floppy_drive_get_flag_state(img, FLOPPY_DRIVE_INDEX))
1957                  {
1958                     /* update index count */
1959                     index_count++;
1960                  }
1961               }
1962               while (index_count!=2);
1963
1964               /* at this point, we have seen a id or two index pulses have occured! */
1965               fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1966               fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1967               fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1968               fdc->upd765_result_bytes[3] = id.C; /* C */
1969               fdc->upd765_result_bytes[4] = id.H; /* H */
1970               fdc->upd765_result_bytes[5] = id.R; /* R */
1971               fdc->upd765_result_bytes[6] = id.N; /* N */
1972
1973               upd765_setup_result_phase(device,7);
1974            }
1975            else
1976            {
1977               /* floppy drive is ready, but no disc is inserted */
1978               /* this occurs on the PC */
1979               /* in this case, the command never quits! */
1980               /* there are no index pulses to stop the command! */
1981            }
1982         }
1983         else
1984         {
1985            /* what are id values when drive not ready? */
1986
1987            /* not ready, abnormal termination */
1988            fdc->upd765_status[0] |= (1<<3) | (1<<6);
1989            fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
1990            fdc->upd765_result_bytes[1] = fdc->upd765_status[1];
1991            fdc->upd765_result_bytes[2] = fdc->upd765_status[2];
1992            fdc->upd765_result_bytes[3] = 0; /* C */
1993            fdc->upd765_result_bytes[4] = 0; /* H */
1994            fdc->upd765_result_bytes[5] = 0; /* R */
1995            fdc->upd765_result_bytes[6] = 0; /* N */
1996         }
1997         break;
1998
1999
2000      case 0x08: /* sense interrupt status */
2001         /* interrupt pending? */
2002         if (fdc->upd765_flags & UPD765_INT)
2003         {
2004            /* yes. Clear int */
2005            upd765_set_int(device, CLEAR_LINE);
2006
2007            /* clear drive seek bits */
2008            fdc->FDC_main &= ~(1 | 2 | 4 | 8);
2009
2010            /* return status */
2011            fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
2012            /* return pcn */
2013            fdc->upd765_result_bytes[1] = fdc->pcn[fdc->drive];
2014
2015            /* return result */
2016            upd765_setup_result_phase(device,2);
2017         }
2018         else
2019         {
2020            if(fdc->version == TYPE_UPD72065 && (fdc->FDC_main & 0x0f) == 0x00)
2021            {  // based on XM6
2022               upd765_setup_invalid(device);
2023            }
2024            else
2025            {
2026               /* no int */
2027               fdc->upd765_result_bytes[0] = 0x80;
2028               /* return pcn */
2029               fdc->upd765_result_bytes[1] = fdc->pcn[fdc->drive];
2030
2031               /* return result */
2032               upd765_setup_result_phase(device,2);
2033            }
2034         }
2035         break;
2036
2037      case 0x06:  /* read data */
2038         upd765_setup_drive_and_side(device);
2039
2040         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2041         fdc->upd765_status[1] = 0;
2042         fdc->upd765_status[2] = 0;
2043
2044         upd765_read_data(device);
2045         break;
2046
2047      case 0x0c:
2048         /* read deleted data */
2049         upd765_setup_drive_and_side(device);
2050
2051         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2052         fdc->upd765_status[1] = 0;
2053         fdc->upd765_status[2] = 0;
2054
2055         /* .. for now */
2056         upd765_read_data(device);
2057         break;
2058
2059      case 0x09:
2060         /* write deleted data */
2061         upd765_setup_drive_and_side(device);
2062
2063         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2064         fdc->upd765_status[1] = 0;
2065         fdc->upd765_status[2] = 0;
2066
2067         /* ... for now */
2068         upd765_write_data(device);
2069         break;
2070
2071      case 0x02:
2072         /* read a track */
2073         upd765_setup_drive_and_side(device);
2074         img = current_image(device);
2075         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2076         fdc->upd765_status[1] = 0;
2077         fdc->upd765_status[2] = 0;
2078
2079         fdc->upd765_status[0] |= UPD765_ST1_NO_DATA;
2080
2081         /* wait for index */
2082         do
2083         {
2084            /* get next id from disc */
2085            floppy_drive_get_next_id(img, fdc->side,&id);
2086         }
2087         while ((floppy_drive_get_flag_state(img, FLOPPY_DRIVE_INDEX))==0);
2088
2089         fdc->sector_counter = 0;
2090
2091         upd765_read_a_track(device);
2092         break;
2093
2094      case 0x05:  /* write data */
2095         upd765_setup_drive_and_side(device);
2096
2097         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2098         fdc->upd765_status[1] = 0;
2099         fdc->upd765_status[2] = 0;
2100
2101         upd765_write_data(device);
2102         break;
2103
2104      case 0x0d:   /* format a track */
2105         upd765_setup_drive_and_side(device);
2106
2107         fdc->upd765_status[0] = fdc->drive | (fdc->side<<2);
2108         fdc->upd765_status[1] = 0;
2109         fdc->upd765_status[2] = 0;
2110
2111         fdc->sector_counter = 0;
2112
2113         upd765_format_track(device);
2114         break;
2115
2116      default:   /* invalid */
2117         switch (fdc->version)
2118         {
2119            case TYPE_UPD765A:
2120               upd765_setup_invalid(device);
2121               break;
2122
2123            case TYPE_UPD765B:
2124               /* from upd765b data sheet */
2125               if ((fdc->upd765_command_bytes[0] & 0x01f)==0x010)
2126               {
2127                  /* version */
2128                  fdc->upd765_status[0] = 0x090;
2129                  fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
2130                  upd765_setup_result_phase(device,1);
2131               }
2132               break;
2133
2134            case TYPE_UPD72065:
2135               switch(fdc->upd765_command_bytes[0] & 0x3f)
2136               {
2137                  case 0x36:  // Software Reset
2138                     upd765_reset(device,0);
2139                     upd765_idle(device);
2140                     if(LOG_COMMAND)
2141                        logerror("upd72065: command - Software Reset\n");
2142                     break;
2143                  default:
2144                     upd765_setup_invalid(device);
2145                     break;
2146               }
2147               break;
2148
2149            case TYPE_SMC37C78:
2150               /* TO BE COMPLETED!!! !*/
2151               switch (fdc->upd765_command_bytes[0] & 0x1f)
2152               {
2153                  case 0x10:      /* version */
2154                     fdc->upd765_status[0] = 0x90;
2155                     fdc->upd765_result_bytes[0] = fdc->upd765_status[0];
2156                     upd765_setup_result_phase(device,1);
2157                     break;
2158
2159                  case 0x13:      /* configure */
2160                     upd765_idle(device);
2161                     break;
2162
2163                  case 0x0e:      /* dump reg */
2164                     fdc->upd765_result_bytes[0] = fdc->pcn[0];
2165                     fdc->upd765_result_bytes[1] = fdc->pcn[1];
2166                     fdc->upd765_result_bytes[2] = fdc->pcn[2];
2167                     fdc->upd765_result_bytes[3] = fdc->pcn[3];
2168
2169                     upd765_setup_result_phase(device,10);
2170                     break;
2171
2172                  case 0x12:      /* perpendicular mode */
2173                     upd765_idle(device);
2174                     break;
2175
2176                  case 0x14:      /* lock */
2177                     upd765_setup_result_phase(device,1);
2178                     break;
2179               }
2180               break;
2181         }
2182   }
2183}
2184
2185
2186/* dma acknowledge write */
2187WRITE8_DEVICE_HANDLER(upd765_dack_w)
2188{
2189   /* clear request */
2190   upd765_set_dma_drq(device, CLEAR_LINE);
2191
2192   /* write data */
2193   upd765_data_w(device, offset, data);
2194}
2195
2196READ8_DEVICE_HANDLER(upd765_dack_r)
2197{
2198   /* clear data request */
2199   upd765_set_dma_drq(device,CLEAR_LINE);
2200
2201   /* read data */
2202   return upd765_data_r(device, offset);
2203}
2204
2205static TIMER_CALLBACK( interrupt_callback )
2206{
2207   device_t* device = (device_t*)ptr;
2208   upd765_set_int(device, 1);
2209}
2210
2211void upd765_reset(device_t *device, int offset)
2212{
2213   upd765_t *fdc = get_safe_token(device);
2214
2215   /* upd765 in idle state - ready to accept commands */
2216   upd765_idle(device);
2217
2218   /* set int low */
2219   upd765_set_int(device, 0);
2220   /* set dma drq output */
2221   upd765_set_dma_drq(device,0);
2222
2223   /* tandy 100hx assumes that after NEC is reset, it is in DMA mode */
2224   fdc->upd765_flags |= UPD765_DMA_MODE;
2225
2226   /* if ready input is set during reset generate an int */
2227   if (fdc->upd765_flags & UPD765_FDD_READY)
2228   {
2229      int i;
2230      int a_drive_is_ready;
2231
2232      fdc->upd765_status[0] = 0x080 | 0x040;
2233
2234      /* for the purpose of pc-xt. If any of the drives have a disk inserted,
2235        do not set not-ready - need to check with pc_fdc->c whether all drives
2236        are checked or only the drive selected with the drive select bits?? */
2237
2238      a_drive_is_ready = 0;
2239      for (i = 0; i < 4; i++)
2240      {
2241         if (fdc->intf->floppy_drive_tags[i]!=NULL)
2242         {
2243            device_t *img;
2244
2245            if (device->owner() != NULL)
2246               img = device->owner()->subdevice(fdc->intf->floppy_drive_tags[i]);
2247            else
2248               img = device->machine().device(fdc->intf->floppy_drive_tags[i]);
2249
2250            device_image_interface *image = dynamic_cast<device_image_interface *>( img);
2251            if (image->exists())
2252            {
2253               a_drive_is_ready = 1;
2254               break;
2255            }
2256         }
2257
2258      }
2259
2260      if (!a_drive_is_ready && fdc->intf->rdy_pin == UPD765_RDY_PIN_CONNECTED )
2261      {
2262         fdc->upd765_status[0] |= 0x08;
2263      }
2264
2265      device->machine().scheduler().timer_set(attotime::from_usec(5), FUNC(interrupt_callback),0,device);
2266   }
2267}
2268
2269WRITE_LINE_DEVICE_HANDLER( upd765_reset_w )
2270{
2271   upd765_t *fdc = get_safe_token(device);
2272
2273   int flags;
2274
2275   /* get previous reset state */
2276   flags = fdc->upd765_flags;
2277
2278   /* set new reset state */
2279   /* clear reset */
2280   fdc->upd765_flags &= ~UPD765_RESET;
2281
2282   /* reset */
2283   if (state)
2284   {
2285      fdc->upd765_flags |= UPD765_RESET;
2286
2287      upd765_set_int(device, 0);
2288   }
2289
2290   /* reset changed state? */
2291   if (((flags^fdc->upd765_flags) & UPD765_RESET)!=0)
2292   {
2293      /* yes */
2294
2295      /* no longer reset */
2296      if ((fdc->upd765_flags & UPD765_RESET)==0)
2297      {
2298         /* reset nec */
2299         upd765_reset(device, 0);
2300      }
2301   }
2302}
2303
2304
2305WRITE_LINE_DEVICE_HANDLER( upd765_ready_w )
2306{
2307   upd765_t *fdc = get_safe_token(device);
2308
2309   /* clear ready state */
2310   fdc->upd765_flags &= ~UPD765_FDD_READY;
2311
2312   if (state)
2313   {
2314      fdc->upd765_flags |= UPD765_FDD_READY;
2315   }
2316}
2317
2318
2319/* Device Interface */
2320
2321static void common_start(device_t *device, int device_type)
2322{
2323   upd765_t *fdc = get_safe_token(device);
2324   // validate arguments
2325
2326   assert(device != NULL);
2327   assert(device->tag() != NULL);
2328   assert(device->static_config() != NULL);
2329
2330   fdc->intf = (const upd765_interface*)device->static_config();
2331
2332   fdc->version = (UPD765_VERSION)device_type;
2333   fdc->timer = device->machine().scheduler().timer_alloc(FUNC(upd765_timer_callback), (void*)device);
2334   fdc->seek_timer = device->machine().scheduler().timer_alloc(FUNC(upd765_seek_timer_callback), (void*)device);
2335   fdc->command_timer = device->machine().scheduler().timer_alloc(FUNC(upd765_continue_command), (void*)device);
2336
2337   fdc->upd765_flags &= UPD765_FDD_READY;
2338   fdc->data_buffer = auto_alloc_array(device->machine(), char, 32*1024);
2339
2340   fdc->out_int_func.resolve(fdc->intf->out_int_func, *device);
2341   fdc->out_drq_func.resolve(fdc->intf->out_drq_func, *device);
2342
2343   // register for state saving
2344   //state_save_register_item(device->machine(), "upd765", device->tag(), 0, upd765->number);
2345}
2346
2347static DEVICE_START( upd765a )
2348{
2349   common_start(device, TYPE_UPD765A);
2350}
2351
2352static DEVICE_START( upd765b )
2353{
2354   common_start(device, TYPE_UPD765B);
2355}
2356
2357static DEVICE_START( smc37c78 )
2358{
2359   common_start(device, TYPE_SMC37C78);
2360}
2361
2362static DEVICE_START( upd72065 )
2363{
2364   common_start(device, TYPE_UPD72065);
2365}
2366
2367static DEVICE_RESET( upd765 )
2368{
2369   int i;
2370   upd765_t *fdc = get_safe_token(device);
2371   for (i = 0; i < 4; i++) {
2372      if (fdc->intf->floppy_drive_tags[i]!=NULL)
2373      {
2374         device_t *img;
2375
2376         if (device->owner() != NULL)
2377            img = device->owner()->subdevice(fdc->intf->floppy_drive_tags[i]);
2378         else
2379            img = device->machine().device(fdc->intf->floppy_drive_tags[i]);
2380
2381         floppy_drive_set_controller(img, device);
2382         floppy_drive_set_ready_state_change_callback(img, upd765_set_ready_change_callback);
2383      }
2384   }
2385
2386   upd765_reset(device,0);
2387}
2388
2389DEVICE_GET_INFO( upd765a )
2390{
2391   switch (state)
2392   {
2393      /* --- the following bits of info are returned as 64-bit signed integers --- */
2394      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(upd765_t);               break;
2395      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
2396
2397      /* --- the following bits of info are returned as pointers to data or functions --- */
2398      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(upd765a);   break;
2399      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
2400      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(upd765);   break;
2401
2402      /* --- the following bits of info are returned as NULL-terminated strings --- */
2403      case DEVINFO_STR_NAME:                     strcpy(info->s, "UPD765A");                  break;
2404      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "UPD765A");                  break;
2405      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                     break;
2406      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                     break;
2407      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright MESS Team");         break;
2408   }
2409}
2410
2411
2412DEVICE_GET_INFO( upd765b )
2413{
2414   switch (state)
2415   {
2416      /* --- the following bits of info are returned as NULL-terminated strings --- */
2417      case DEVINFO_STR_NAME:                     strcpy(info->s, "UPD765B");            break;
2418
2419      /* --- the following bits of info are returned as pointers to data or functions --- */
2420      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(upd765b);   break;
2421
2422      default:                              DEVICE_GET_INFO_CALL(upd765a);            break;
2423   }
2424}
2425
2426DEVICE_GET_INFO( smc37c78 )
2427{
2428   switch (state)
2429   {
2430      /* --- the following bits of info are returned as NULL-terminated strings --- */
2431      case DEVINFO_STR_NAME:                     strcpy(info->s, "SMC37C78");            break;
2432
2433      /* --- the following bits of info are returned as pointers to data or functions --- */
2434      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(smc37c78);   break;
2435
2436      default:                              DEVICE_GET_INFO_CALL(upd765a);            break;
2437   }
2438}
2439
2440DEVICE_GET_INFO( upd72065 )
2441{
2442   switch (state)
2443   {
2444      /* --- the following bits of info are returned as NULL-terminated strings --- */
2445      case DEVINFO_STR_NAME:                     strcpy(info->s, "UPD72065");            break;
2446
2447      /* --- the following bits of info are returned as pointers to data or functions --- */
2448      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(upd72065);   break;
2449
2450      default:                              DEVICE_GET_INFO_CALL(upd765a);            break;
2451   }
2452}
2453
2454DEFINE_LEGACY_DEVICE(UPD765A, upd765a);
2455DEFINE_LEGACY_DEVICE(UPD765B, upd765b);
2456DEFINE_LEGACY_DEVICE(SMC37C78, smc37c78);
2457DEFINE_LEGACY_DEVICE(UPD72065, upd72065);
src/mess/machine/upd765.h
r0r11474
1/***************************************************************************
2
3    machine/upd765.h
4
5    Functions to emulate a NEC upd765/Intel 8272 compatible
6    floppy disk controller
7
8***************************************************************************/
9
10#ifndef __UPD765_H__
11#define __UPD765_H__
12
13#include "devcb.h"
14#include "imagedev/flopdrv.h"
15
16
17/***************************************************************************
18    MACROS
19***************************************************************************/
20
21DECLARE_LEGACY_DEVICE(UPD765A, upd765a);
22DECLARE_LEGACY_DEVICE(UPD765B, upd765b);
23DECLARE_LEGACY_DEVICE(SMC37C78, smc37c78);
24DECLARE_LEGACY_DEVICE(UPD72065, upd72065);
25
26/***************************************************************************
27    TYPE DEFINITIONS
28***************************************************************************/
29
30/* RDY pin connected state */
31typedef enum
32{
33   UPD765_RDY_PIN_NOT_CONNECTED = 0,
34   UPD765_RDY_PIN_CONNECTED = 1
35} UPD765_RDY_PIN;
36
37#define UPD765_DAM_DELETED_DATA 0x0f8
38#define UPD765_DAM_DATA 0x0fb
39
40typedef device_t *(*upd765_get_image_func)(device_t *device, int floppy_index);
41#define UPD765_GET_IMAGE(name)   device_t *name(device_t *device, int floppy_index )
42
43
44typedef struct upd765_interface
45{
46   /* interrupt issued */
47   devcb_write_line out_int_func;
48
49   /* dma data request */
50   devcb_write_line out_drq_func;
51
52   /* image lookup */
53   upd765_get_image_func get_image;
54
55   UPD765_RDY_PIN rdy_pin;
56
57   const char *floppy_drive_tags[4];
58} upd765_interface;
59
60
61/***************************************************************************
62    FUNCTION PROTOTYPES
63***************************************************************************/
64
65/* read of data register */
66READ8_DEVICE_HANDLER(upd765_data_r);
67/* write to data register */
68WRITE8_DEVICE_HANDLER(upd765_data_w);
69/* read of main status register */
70READ8_DEVICE_HANDLER(upd765_status_r);
71
72/* dma acknowledge with write */
73WRITE8_DEVICE_HANDLER(upd765_dack_w);
74/* dma acknowledge with read */
75READ8_DEVICE_HANDLER(upd765_dack_r);
76
77/* reset upd765 */
78void upd765_reset(device_t *device, int);
79
80/* reset pin of upd765 */
81WRITE_LINE_DEVICE_HANDLER(upd765_reset_w);
82
83/* set upd765 terminal count input state */
84WRITE_LINE_DEVICE_HANDLER(upd765_tc_w);
85
86/* set upd765 ready input*/
87WRITE_LINE_DEVICE_HANDLER(upd765_ready_w);
88
89void upd765_idle(device_t *device);
90
91/*********************/
92/* STATUS REGISTER 1 */
93
94/* this is set if a TC signal was not received after the sector data was read */
95#define UPD765_ST1_END_OF_CYLINDER (1<<7)
96/* this is set if the sector ID being searched for is not found */
97#define UPD765_ST1_NO_DATA (1<<2)
98/* set if disc is write protected and a write/format operation was performed */
99#define UPD765_ST1_NOT_WRITEABLE (1<<1)
100
101/*********************/
102/* STATUS REGISTER 2 */
103
104/* C parameter specified did not match C value read from disc */
105#define UPD765_ST2_WRONG_CYLINDER (1<<4)
106/* C parameter specified did not match C value read from disc, and C read from disc was 0x0ff */
107#define UPD765_ST2_BAD_CYLINDER (1<<1)
108/* this is set if the FDC encounters a Deleted Data Mark when executing a read data
109command, or FDC encounters a Data Mark when executing a read deleted data command */
110#define UPD765_ST2_CONTROL_MARK (1<<6)
111
112/***************************************************************************
113    DEVICE CONFIGURATION MACROS
114***************************************************************************/
115
116#define MCFG_UPD765A_ADD(_tag, _intrf) \
117   MCFG_DEVICE_ADD(_tag, UPD765A, 0) \
118   MCFG_DEVICE_CONFIG(_intrf)
119
120#define MCFG_UPD765A_MODIFY(_tag, _intrf) \
121  MCFG_DEVICE_MODIFY(_tag)         \
122  MCFG_DEVICE_CONFIG(_intrf)
123
124#define MCFG_UPD765A_REMOVE(_tag)      \
125  MCFG_DEVICE_REMOVE(_tag)
126
127#define MCFG_UPD765B_ADD(_tag, _intrf) \
128   MCFG_DEVICE_ADD(_tag, UPD765B, 0) \
129   MCFG_DEVICE_CONFIG(_intrf)
130
131#define MCFG_UPD765B_MODIFY(_tag, _intrf) \
132  MCFG_DEVICE_MODIFY(_tag)         \
133  MCFG_DEVICE_CONFIG(_intrf)
134
135#define MCFG_UPD765B_REMOVE(_tag)      \
136  MCFG_DEVICE_REMOVE(_tag)
137
138#define MCFG_SMC37C78_ADD(_tag, _intrf) \
139   MCFG_DEVICE_ADD(_tag, SMC37C78, 0) \
140   MCFG_DEVICE_CONFIG(_intrf)
141
142#define MCFG_SMC37C78_MODIFY(_tag, _intrf) \
143  MCFG_DEVICE_MODIFY(_tag)         \
144  MCFG_DEVICE_CONFIG(_intrf)
145
146#define MCFG_SMC37C78_REMOVE(_tag)      \
147  MCFG_DEVICE_REMOVE(_tag)
148
149#define MCFG_UPD72065_ADD(_tag, _intrf) \
150   MCFG_DEVICE_ADD(_tag, UPD72065, 0) \
151   MCFG_DEVICE_CONFIG(_intrf)
152
153#define MCFG_UPD72065_MODIFY(_tag, _intrf) \
154  MCFG_DEVICE_MODIFY(_tag)         \
155  MCFG_DEVICE_CONFIG(_intrf)
156
157#define MCFG_UPD72065_REMOVE(_tag)      \
158  MCFG_DEVICE_REMOVE(_tag)
159
160
161#endif /* __UPD765_H__ */
src/mess/machine/serial.c
r0r11474
1/* internal serial transmission */
2
3/* select a file on host filesystem to transfer using serial method,
4setup serial interface software in driver and let the transfer begin */
5
6/* this is used in the Amstrad NC Notepad emulation */
7
8
9
10/*
11
12    the output starts at 1 level. It changes to 0 when the start bit has been transmitted.
13    This therefore signals that data is following.
14    When all data bits have been received, stop bits are transmitted with a value of 1.
15
16    msm8251 expects this in asynchronous mode:
17    packet format:
18
19    bit count           function        value
20    1                   start bit       0
21    note 1              data bits       x
22    note 2              parity bit      x
23    note 3              stop bits       1
24
25    Note:
26    1. Data size can be defined (usual value is 8)
27    2. Parity bit (if parity is set to odd or even). Value of bit
28        is defined by data parity.
29    3. There should be at least 1 stop bit.
30*/
31
32#include "emu.h"
33#include "serial.h"
34
35
36#define VERBOSE 0
37#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
38
39/* a read/write bit stream. used to transmit data and to receive data */
40typedef struct _data_stream data_stream;
41struct _data_stream
42{
43   /* pointer to buffer */
44   unsigned char *pData;
45   /* length of buffer */
46   unsigned long DataLength;
47
48   /* bit offset within current byte */
49   unsigned long BitCount;
50   /* byte offset within data */
51   unsigned long ByteCount;
52};
53
54typedef struct _serial_t serial_t;
55struct _serial_t
56{
57   /* transmit data bit-stream */
58   data_stream transmit;
59   /* receive data bit-stream */
60   data_stream receive;
61
62   /* register to receive data */
63   serial_receive_register   receive_reg;
64   /* register to transmit data */
65   serial_transmit_register transmit_reg;
66
67   /* connection to transmit/receive data over */
68   serial_connection connection;
69
70   /* data form to transmit/receive */
71   data_form form;
72
73   int transmit_state;
74
75   /* baud rate */
76   unsigned long BaudRate;
77
78   /* baud rate timer */
79   emu_timer   *timer;
80};
81
82INLINE serial_t *get_safe_token(device_t *device)
83{
84   assert(device != NULL);
85   //assert(device->type() == SERIAL);
86
87   return (serial_t *)downcast<legacy_device_base *>(device)->token();
88}
89
90
91/* the serial streams */
92static TIMER_CALLBACK(serial_device_baud_rate_callback);
93
94
95/*********************************************************/
96
97static unsigned char serial_parity_table[256];
98
99void serial_helper_setup(void)
100{
101   int i;
102
103   /* if sum of all bits in the byte is even, then the data
104    has even parity, otherwise it has odd parity */
105   for (i=0; i<256; i++)
106   {
107      int data;
108      int sum;
109      int b;
110
111      sum = 0;
112      data = i;
113
114      for (b=0; b<8; b++)
115      {
116         sum+=data & 0x01;
117
118         data = data>>1;
119      }
120
121      serial_parity_table[i] = sum & 0x01;
122   }
123}
124
125static unsigned char serial_helper_get_parity(unsigned char data)
126{
127   return serial_parity_table[data & 0x0ff];
128}
129
130static void serial_device_in_callback(running_machine &machine, int id, unsigned long status)
131{
132/*  serial_t *ser = get_safe_token(device);
133
134    ser->connection.input_state = status;*/
135}
136
137/***** SERIAL DEVICE ******/
138void serial_device_setup(device_t *device, int baud_rate, int num_data_bits, int stop_bit_count, int parity_code)
139{
140   serial_t *ser = get_safe_token(device);
141
142   ser->BaudRate = baud_rate;
143   ser->form.word_length = num_data_bits;
144   ser->form.stop_bit_count = stop_bit_count;
145   ser->form.parity = parity_code;
146   ser->timer = device->machine().scheduler().timer_alloc(FUNC(serial_device_baud_rate_callback), (void *)device);
147
148   serial_connection_init(device->machine(),&ser->connection);
149   serial_connection_set_in_callback(device->machine(),&ser->connection, serial_device_in_callback);
150
151   /* signal to other end it is clear to send! */
152   /* data is initially high state */
153   /* set /rts */
154   ser->connection.State |= SERIAL_STATE_RTS;
155   /* signal to other end data is ready to be accepted */
156   /* set /dtr */
157   ser->connection.State |= SERIAL_STATE_DTR;
158   set_out_data_bit(ser->connection.State, 1);
159   serial_connection_out(device->machine(),&ser->connection);
160   transmit_register_reset(&ser->transmit_reg);
161   receive_register_reset(&ser->receive_reg);
162   receive_register_setup(&ser->receive_reg, &ser->form);
163}
164
165
166unsigned long serial_device_get_state(device_t *device)
167{
168   serial_t *ser = get_safe_token(device);
169
170   return ser->connection.State;
171}
172
173void serial_device_set_transmit_state(device_t *device, int state)
174{
175   int previous_state;
176   serial_t *ser = get_safe_token(device);
177
178   previous_state = ser->transmit_state;
179
180   ser->transmit_state = state;
181
182   if ((state^previous_state)!=0)
183   {
184      if (state)
185      {
186         /* start timer */
187         ser->timer->adjust(attotime::zero, 0, attotime::from_hz(ser->BaudRate));
188      }
189      else
190      {
191         /* remove timer */
192         ser->timer->reset();
193      }
194   }
195
196}
197
198/* get a bit from input stream */
199static int data_stream_get_data_bit_from_data_byte(data_stream *stream)
200{
201   int data_bit;
202   int data_byte;
203
204   if (stream->ByteCount<stream->DataLength)
205   {
206      /* get data from buffer */
207      data_byte= stream->pData[stream->ByteCount];
208   }
209   else
210   {
211      /* over end of buffer, so return 0 */
212      data_byte= 0;
213   }
214
215   /* get bit from data */
216   data_bit = (data_byte>>(7-stream->BitCount)) & 0x01;
217
218   /* update bit count */
219   stream->BitCount++;
220   /* ripple overflow onto byte count */
221   stream->ByteCount+=stream->BitCount>>3;
222   /* lock bit count into range */
223   stream->BitCount &=0x07;
224
225   /* do not let it read over end of data */
226   if (stream->ByteCount>=stream->DataLength)
227   {
228      stream->ByteCount = stream->DataLength-1;
229   }
230
231   return data_bit;
232}
233
234
235void receive_register_setup(serial_receive_register *receive, data_form *data_form)
236{
237   receive->bit_count = data_form->word_length + data_form->stop_bit_count;
238
239   if (data_form->parity != SERIAL_PARITY_NONE)
240   {
241      receive->bit_count++;
242   }
243}
244
245
246/* this is generic code to be used in serial chip implementations */
247/* the majority of serial chips work in the same way and this code will work */
248/* for them */
249
250/* receive a bit */
251void receive_register_update_bit(serial_receive_register *receive, int bit)
252{
253   int previous_bit;
254
255   LOG(("receive register receive bit: %1x\n",bit));
256   previous_bit = receive->register_data & 1;
257
258   /* shift previous bit 7 out */
259   receive->register_data = receive->register_data<<1;
260   /* shift new bit in */
261   receive->register_data = (receive->register_data & 0xfffe) | bit;
262   /* update bit count received */
263   receive->bit_count_received++;
264
265   /* asyncrhonouse mode */
266   if (receive->flags & RECEIVE_REGISTER_WAITING_FOR_START_BIT)
267   {
268      /* the previous bit is stored in uart.receive char bit 0 */
269      /* has the bit state changed? */
270      if (((previous_bit ^ bit) & 0x01)!=0)
271      {
272         /* yes */
273         if (bit==0)
274         {
275            //logerror("receive register saw start bit\n");
276
277            /* seen start bit! */
278            /* not waiting for start bit now! */
279            receive->flags &=~RECEIVE_REGISTER_WAITING_FOR_START_BIT;
280            receive->flags |=RECEIVE_REGISTER_SYNCHRONISED;
281            /* reset bit count received */
282            receive->bit_count_received = 0;
283         }
284      }
285   }
286   else
287   if (receive->flags & RECEIVE_REGISTER_SYNCHRONISED)
288   {
289      /* received all bits? */
290      if (receive->bit_count_received==receive->bit_count)
291      {
292         receive->bit_count_received = 0;
293         receive->flags &=~RECEIVE_REGISTER_SYNCHRONISED;
294         receive->flags |= RECEIVE_REGISTER_WAITING_FOR_START_BIT;
295         //logerror("receive register full\n");
296         receive->flags |= RECEIVE_REGISTER_FULL;
297      }
298   }
299}
300
301void receive_register_reset(serial_receive_register *receive_reg)
302{
303   receive_reg->bit_count_received = 0;
304   receive_reg->flags &=~RECEIVE_REGISTER_FULL;
305   receive_reg->flags &=~RECEIVE_REGISTER_SYNCHRONISED;
306   receive_reg->flags |= RECEIVE_REGISTER_WAITING_FOR_START_BIT;
307}
308
309void receive_register_extract(serial_receive_register *receive_reg, data_form *data_form)
310{
311   unsigned long data_shift;
312   UINT8 data;
313
314   receive_register_reset(receive_reg);
315
316   data_shift = 0;
317
318   /* if parity is even or odd, there should be a parity bit in the stream! */
319   if (data_form->parity!=SERIAL_PARITY_NONE)
320   {
321      data_shift++;
322   }
323
324   data_shift+=data_form->stop_bit_count;
325
326   /* strip off stop bits and parity */
327   data = receive_reg->register_data>>data_shift;
328
329   /* mask off other bits so data byte has 0's in unused bits */
330   data = data & (0x0ff
331      >>
332      (8-(data_form->word_length)));
333
334   receive_reg->byte_received  = data;
335
336   /* parity enable? */
337   switch (data_form->parity)
338   {
339      case SERIAL_PARITY_NONE:
340         break;
341
342      /* check parity */
343      case SERIAL_PARITY_ODD:
344      case SERIAL_PARITY_EVEN:
345      {
346         //unsigned char computed_parity;
347         //unsigned char parity_received;
348
349         /* get state of parity bit received */
350         //parity_received = (receive_reg->register_data>>data_form->stop_bit_count) & 0x01;
351
352         /* compute parity for received bits */
353         //computed_parity = serial_helper_get_parity(data);
354
355         if (data_form->parity == SERIAL_PARITY_ODD)
356         {
357            /* odd parity */
358
359
360         }
361         else
362         {
363            /* even parity */
364
365
366         }
367
368      }
369      break;
370   }
371}
372
373/***** TRANSMIT REGISTER *****/
374
375void transmit_register_reset(serial_transmit_register *transmit_reg)
376{
377   transmit_reg->flags |=TRANSMIT_REGISTER_EMPTY;
378}
379
380/* used to construct data in stream format */
381static void transmit_register_add_bit(serial_transmit_register *transmit_reg, int bit)
382{
383   /* combine bit */
384   transmit_reg->register_data = transmit_reg->register_data<<1;
385   transmit_reg->register_data &=~1;
386   transmit_reg->register_data|=(bit & 0x01);
387   transmit_reg->bit_count++;
388}
389
390
391/* generate data in stream format ready for transfer */
392void transmit_register_setup(serial_transmit_register *transmit_reg, data_form *data_form,unsigned char data_byte)
393{
394   int i;
395   unsigned char transmit_data;
396
397
398   transmit_reg->bit_count_transmitted = 0;
399   transmit_reg->bit_count = 0;
400   transmit_reg->flags &=~TRANSMIT_REGISTER_EMPTY;
401
402   /* start bit */
403   transmit_register_add_bit(transmit_reg,0);
404
405   /* data bits */
406   transmit_data = data_byte;
407   for (i=0; i<data_form->word_length; i++)
408   {
409      int databit;
410
411      /* get bit from data */
412      databit = (transmit_data>>(data_form->word_length-1)) & 0x01;
413      /* add bit to formatted byte */
414      transmit_register_add_bit(transmit_reg, databit);
415      transmit_data = transmit_data<<1;
416   }
417
418   /* parity */
419   if (data_form->parity!=SERIAL_PARITY_NONE)
420   {
421      /* odd or even parity */
422      unsigned char parity;
423
424      /* get parity */
425      /* if parity = 0, data has even parity - i.e. there is an even number of one bits in the data */
426      /* if parity = 1, data has odd parity - i.e. there is an odd number of one bits in the data */
427      parity = serial_helper_get_parity(data_byte);
428
429      transmit_register_add_bit(transmit_reg, parity);
430   }
431
432   /* stop bit(s) */
433   for (i=0; i<data_form->stop_bit_count; i++)
434   {
435      transmit_register_add_bit(transmit_reg,1);
436   }
437}
438
439
440/* get a bit from the transmit register */
441static int transmit_register_get_data_bit(serial_transmit_register *transmit_reg)
442{
443   int bit;
444
445   bit = (transmit_reg->register_data>>
446      (transmit_reg->bit_count - 1 -
447      transmit_reg->bit_count_transmitted)) & 0x01;
448
449   transmit_reg->bit_count_transmitted++;
450
451   /* have all bits of this stream formatted byte been sent? */
452   if (transmit_reg->bit_count_transmitted==transmit_reg->bit_count)
453   {
454      /* yes - generate a new byte to send */
455      transmit_reg->flags |= TRANSMIT_REGISTER_EMPTY;
456   }
457
458   return bit;
459}
460
461
462void transmit_register_send_bit(running_machine &machine, serial_transmit_register *transmit_reg, serial_connection *connection)
463{
464   int data;
465
466   data = transmit_register_get_data_bit(transmit_reg);
467
468   /* set tx data bit */
469   set_out_data_bit(connection->State, data);
470
471   /* state out through connection */
472   serial_connection_out(machine, connection);
473}
474
475static void serial_protocol_none_sent_char(device_t *device)
476{
477   int i;
478   int bit;
479   unsigned char data_byte;
480   serial_t *ser = get_safe_token(device);
481
482   /* generate byte to transmit */
483   data_byte = 0;
484   for (i=0; i<ser->form.word_length; i++)
485   {
486      data_byte = data_byte<<1;
487      bit = data_stream_get_data_bit_from_data_byte(&ser->transmit);
488      data_byte = data_byte|bit;
489   }
490   /* setup register */
491   transmit_register_setup(&ser->transmit_reg,&ser->form, data_byte);
492
493   logerror("serial device transmitted char: %02x\n",data_byte);
494}
495
496static TIMER_CALLBACK(serial_device_baud_rate_callback)
497{
498   serial_t *ser = get_safe_token((device_t*)ptr);
499
500   /* receive data into receive register */
501   receive_register_update_bit(&ser->receive_reg, get_in_data_bit(ser->connection.input_state));
502
503   if (ser->receive_reg.flags & RECEIVE_REGISTER_FULL)
504   {
505      //logerror("SERIAL DEVICE\n");
506      receive_register_extract(&ser->receive_reg, &ser->form);
507
508      logerror("serial device receive char: %02x\n",ser->receive_reg.byte_received);
509   }
510
511   /* is transmit empty? */
512   if (ser->transmit_reg.flags & TRANSMIT_REGISTER_EMPTY)
513   {
514      /* char has been sent, execute callback */
515      serial_protocol_none_sent_char((device_t*)ptr);
516   }
517
518   /* other side says it is clear to send? */
519   if (ser->connection.input_state & SERIAL_STATE_CTS)
520   {
521      /* send bit */
522      transmit_register_send_bit(machine, &ser->transmit_reg, &ser->connection);
523   }
524}
525
526/* connect the specified connection to this serial device */
527void serial_device_connect(device_t *device, serial_connection *connection)
528{
529   serial_t *ser = get_safe_token(device);
530   serial_connection_link(device->machine(), connection, &ser->connection);
531}
532
533
534/* load image */
535static int serial_device_load_internal(device_image_interface &image, unsigned char **ptr, int *pDataSize)
536{
537   int datasize;
538   unsigned char *data;
539
540   /* get file size */
541   datasize = image.length();
542
543   if (datasize!=0)
544   {
545      /* malloc memory for this data */
546      data = (unsigned char *)malloc(datasize);
547
548      if (data!=NULL)
549      {
550         /* read whole file */
551         image.fread(data, datasize);
552
553         *ptr = data;
554         *pDataSize = datasize;
555
556         logerror("File loaded!\r\n");
557
558         /* ok! */
559         return 1;
560      }
561   }
562   return 0;
563}
564
565/* reset position in stream */
566static void data_stream_reset(data_stream *stream)
567{
568   /* reset byte offset */
569   stream->ByteCount= 0;
570   /* reset bit count */
571   stream->BitCount = 0;
572}
573
574/* free stream */
575static void data_stream_free(data_stream *stream)
576{
577   if (stream->pData!=NULL)
578   {
579      free(stream->pData);
580      stream->pData = NULL;
581   }
582   stream->DataLength = 0;
583}
584
585/* initialise stream */
586static void data_stream_init(data_stream *stream, unsigned char *pData, unsigned long DataLength)
587{
588   stream->pData = pData;
589   stream->DataLength = DataLength;
590   data_stream_reset(stream);
591}
592
593DEVICE_START(serial)
594{
595   //serial_t *ser = get_safe_token(device);
596}
597
598DEVICE_RESET(serial)
599{
600//  serial_t *ser = get_safe_token(device);
601}
602
603DEVICE_IMAGE_LOAD(serial)
604{
605   int data_length;
606   unsigned char *data;
607   device_t *device = &image.device();
608
609   serial_t *ser = get_safe_token(device);
610
611   /* load file and setup transmit data */
612   if (serial_device_load_internal(image, &data, &data_length))
613   {
614      data_stream_init(&ser->transmit, data, data_length);
615      return IMAGE_INIT_PASS;
616   }
617
618   return IMAGE_INIT_FAIL;
619}
620
621
622DEVICE_IMAGE_UNLOAD(serial)
623{
624   device_t *device = &image.device();
625   serial_t *ser = get_safe_token(device);
626
627   /* stop transmit */
628   serial_device_set_transmit_state(device, 0);
629   /* free streams */
630   data_stream_free(&ser->transmit);
631   data_stream_free(&ser->receive);
632}
633
634
635DEVICE_GET_INFO( serial )
636{
637   switch ( state )
638   {
639      case DEVINFO_INT_TOKEN_BYTES:            info->i = sizeof(serial_t);                        break;
640      case DEVINFO_INT_INLINE_CONFIG_BYTES:      info->i = 0;                                 break;
641      case DEVINFO_INT_IMAGE_TYPE:               info->i = IO_SERIAL;                                   break;
642      case DEVINFO_INT_IMAGE_READABLE:            info->i = 1;                                           break;
643      case DEVINFO_INT_IMAGE_WRITEABLE:         info->i = 1;                                           break;
644      case DEVINFO_INT_IMAGE_CREATABLE:          info->i = 1;                                           break;
645
646      case DEVINFO_FCT_START:                      info->start = DEVICE_START_NAME( serial );             break;
647      case DEVINFO_FCT_RESET:                  info->reset = DEVICE_RESET_NAME( serial );            break;
648      case DEVINFO_FCT_IMAGE_LOAD:              info->f = (genf *) DEVICE_IMAGE_LOAD_NAME( serial );   break;
649      case DEVINFO_FCT_IMAGE_UNLOAD:              info->f = (genf *) DEVICE_IMAGE_UNLOAD_NAME(serial );   break;
650      case DEVINFO_STR_NAME:                      strcpy( info->s, "Serial port");                       break;
651      case DEVINFO_STR_FAMILY:                    strcpy(info->s, "Serial port");                           break;
652      case DEVINFO_STR_IMAGE_FILE_EXTENSIONS:       strcpy(info->s, "");                                    break;
653      case DEVINFO_STR_VERSION:               strcpy(info->s, "1.0");                           break;
654      case DEVINFO_STR_SOURCE_FILE:            strcpy(info->s, __FILE__);                        break;
655      case DEVINFO_STR_CREDITS:               strcpy(info->s, "Copyright the MESS Team");          break;
656   }
657}
658
659/*******************************************************************************/
660/*******************************************************************************/
661/********* SERIAL CONNECTION ***********/
662
663
664/* this converts state at this end to a state the other end can accept */
665/* e.g. CTS at this end becomes RTS at other end.
666        RTS at this end becomes CTS at other end.
667        TX at this end becomes RX at other end.
668        RX at this end becomes TX at other end.
669        etc
670
671        The same thing is done inside the serial null-terminal lead */
672
673static unsigned long serial_connection_spin_bits(unsigned long input_status)
674{
675
676   return
677      /* cts -> rts */
678      (((input_status & 0x01)<<1) |
679      /* rts -> cts */
680      ((input_status>>1) & 0x01) |
681      /* dsr -> dtr */
682      (((input_status>>2) & 0x01)<<3) |
683      /* dtr -> dsr */
684      (((input_status>>3) & 0x01)<<2) |
685      /* rx -> tx */
686      (((input_status>>4) & 0x01)<<5) |
687      /* tx -> rx */
688      (((input_status>>5) & 0x01)<<4));
689}
690
691
692/* setup callbacks for connection */
693void serial_connection_init(running_machine &machine, serial_connection *connection)
694{
695   connection->out_callback = NULL;
696   connection->in_callback = NULL;
697}
698
699/* set callback which will be executed when in status has changed */
700void serial_connection_set_in_callback(running_machine &machine, serial_connection *connection, void (*in_cb)(running_machine &machine, int id, unsigned long status))
701{
702   connection->in_callback = in_cb;
703}
704
705/* output new state through connection */
706void serial_connection_out(running_machine &machine, serial_connection *connection)
707{
708   if (connection->out_callback!=NULL)
709   {
710      unsigned long state_at_other_end;
711
712      state_at_other_end = serial_connection_spin_bits(connection->State);
713
714      connection->out_callback(machine, connection->id, state_at_other_end);
715   }
716}
717
718/* join two serial connections together */
719void serial_connection_link(running_machine &machine, serial_connection *connection_a, serial_connection *connection_b)
720{
721   /* both connections should have their in connection setup! */
722   /* the in connection is the callback they use to update their state based
723    on the output from the other side */
724   connection_a->out_callback = connection_b->in_callback;
725   connection_b->out_callback = connection_a->in_callback;
726
727   /* let b know the state of a */
728   serial_connection_out(machine,connection_a);
729   /* let a know the state of b */
730   serial_connection_out(machine,connection_b);
731}
732
733DEFINE_LEGACY_IMAGE_DEVICE(SERIAL, serial);
src/mess/machine/serial.h
r0r11474
1/*****************************************************************************
2 *
3 * machine/serial.h
4 *
5 * internal serial transmission
6 *
7 * This code is used to transmit a file stored on the host filesystem
8 * (e.g. PC harddrive) to an emulated system.
9 *
10 * The file is converted into a serial bit-stream which can be received
11 * by the emulated serial chip in the emulated system.
12 *
13 * The file can be transmitted using different protocols.
14 *
15 * A and B are two computers linked with a serial connection
16 * A and B can transmit and receive data, through the same connection
17 *
18 * These flags apply to A and B, and give the state of the input & output
19 * signals at each side.
20 *
21 ****************************************************************************/
22
23#ifndef SERIAL_H_
24#define SERIAL_H_
25
26
27/*
28    CTS = Clear to Send. (INPUT)
29    Other end of connection is ready to accept data
30
31
32    NOTE:
33
34      This output is active low on serial chips (e.g. 0 is CTS is set),
35      but here it is active high!
36*/
37#define SERIAL_STATE_CTS   0x0001
38
39/*
40    RTS = Request to Send. (OUTPUT)
41    This end is ready to send data, and requests if the other
42    end is ready to accept it
43
44    NOTE:
45
46      This output is active low on serial chips (e.g. 0 is RTS is set),
47      but here it is active high!
48*/
49#define SERIAL_STATE_RTS   0x0002
50
51/*
52    DSR = Data Set ready. (INPUT)
53    Other end of connection has data
54
55
56    NOTE:
57
58      This output is active low on serial chips (e.g. 0 is DSR is set),
59      but here it is active high!
60*/
61#define SERIAL_STATE_DSR   0x0004
62
63/*
64    DTR = Data terminal Ready. (OUTPUT)
65    TX contains new data.
66
67    NOTE:
68
69      This output is active low on serial chips (e.g. 0 is DTR is set),
70      but here it is active high!
71*/
72#define SERIAL_STATE_DTR   0x0008
73/* RX = Recieve data. (INPUT) */
74#define SERIAL_STATE_RX_DATA   0x00010
75/* TX = Transmit data. (OUTPUT) */
76#define SERIAL_STATE_TX_DATA   0x00020
77
78/* parity selections */
79/* if all the bits are added in a byte, if the result is:
80    even -> parity is even
81    odd -> parity is odd
82*/
83enum
84{
85   SERIAL_PARITY_NONE,      /* no parity. a parity bit will not be in the transmitted/received data */
86   SERIAL_PARITY_ODD,      /* odd parity */
87   SERIAL_PARITY_EVEN      /* even parity */
88};
89
90/* this macro is used to extract the received data from the status */
91#define get_in_data_bit(x) ((x & SERIAL_STATE_RX_DATA)>>4)
92
93/* this macro is used to set the transmitted data in the status */
94#define set_out_data_bit(x, data) \
95   x&=~SERIAL_STATE_TX_DATA; \
96   x|=(data<<5)
97
98
99/*******************************************************************************/
100/**** SERIAL CONNECTION ***/
101
102
103/* this structure represents a serial connection */
104typedef struct _serial_connection serial_connection;
105struct _serial_connection
106{
107   int id;
108   /* state of this side */
109   unsigned long State;
110
111   /* state of other side - store here */
112   unsigned long input_state;
113
114   /* this callback is executed when this side has refreshed it's state,
115    to let the other end know */
116   void   (*out_callback)(running_machine &machine, int id, unsigned long state);
117   /* this callback is executed when the other side has refreshed it's state,
118    to let the other end know */
119   void   (*in_callback)(running_machine &machine, int id, unsigned long state);
120};
121
122/*----------- defined in machine/serial.c -----------*/
123
124/* setup out and in callbacks */
125void serial_connection_init(running_machine &machine, serial_connection *connection);
126
127/* set callback which will be executed when in status has changed */
128void serial_connection_set_in_callback(running_machine &machine, serial_connection *connection, void (*in_cb)(running_machine &machine, int id, unsigned long status));
129
130/* output status, if callback is setup it will be executed with the new status */
131void serial_connection_out(running_machine &machine, serial_connection *connection);
132
133/* join two serial connections */
134void serial_connection_link(running_machine &machine, serial_connection *connection_a, serial_connection *connection_b);
135
136
137/*******************************************************************************/
138
139/* form of data being transmitted and received */
140typedef struct _data_form data_form;
141struct _data_form
142{
143   /* length of word in bits */
144   unsigned long word_length;
145   /* parity state */
146   unsigned long parity;
147   /* number of stop bits */
148   unsigned long stop_bit_count;
149};
150
151/*******************************************************************************/
152
153/*******************************************************************************/
154/**** RECEIVE AND TRANSMIT GENERIC CODE ****/
155
156/* this can be used by most of the serial chip implementations,
157because they all work in roughly the same way.
158There is generic code to send and receive data in the specified form */
159
160/* receive is waiting for start bit. The transition from high-low indicates
161start of start bit. This is used to synchronise with the data being transfered */
162#define RECEIVE_REGISTER_WAITING_FOR_START_BIT 0x01
163/* receive is synchronised with data, data bits will be clocked in */
164#define RECEIVE_REGISTER_SYNCHRONISED 0x02
165/* set if receive register has been filled */
166#define RECEIVE_REGISTER_FULL 0x04
167
168
169/* the receive register holds data in receive form! */
170/* this must be extracted to get the data byte received */
171typedef struct _serial_receive_register serial_receive_register;
172struct _serial_receive_register
173{
174   /* data */
175   unsigned long register_data;
176   /* flags */
177   unsigned long flags;
178   /* bit count received */
179   unsigned long bit_count_received;
180   /* length of data to receive - includes data bits, parity bit and stop bit */
181   unsigned long bit_count;
182
183   /* the byte of data received */
184   unsigned char byte_received;
185};
186
187void   receive_register_setup(serial_receive_register *receive, data_form *data_form);
188void   receive_register_update_bit(serial_receive_register *receive, int bit_state);
189void   receive_register_extract(serial_receive_register *receive_reg, data_form *data_form);
190void   receive_register_reset(serial_receive_register *receive_reg);
191
192/* the transmit register is the final stage
193in the serial transmit procedure */
194/* normally, data is written to the transmit reg,
195then it is assembled into transmit form and transmitted */
196/* the transmit register holds data in transmit form */
197
198/* register is empty and ready to be filled with data */
199#define TRANSMIT_REGISTER_EMPTY 0x0001
200
201typedef struct _serial_transmit_register serial_transmit_register;
202struct _serial_transmit_register
203{
204   /* data */
205   unsigned long register_data;
206   /* flags */
207   unsigned long flags;
208   /* number of bits transmitted */
209   unsigned long bit_count_transmitted;
210   /* length of data to send */
211   unsigned long bit_count;
212};
213
214/* setup transmit reg ready for transmit */
215void transmit_register_setup(serial_transmit_register *transmit_reg, data_form *data_form,unsigned char data_byte);
216void   transmit_register_send_bit(running_machine &machine, serial_transmit_register *transmit_reg, serial_connection *connection);
217void   transmit_register_reset(serial_transmit_register *transmit_reg);
218
219/*******************************************************************************/
220/**** SERIAL HELPER ****/
221
222void serial_helper_setup(void);
223
224/*******************************************************************************/
225/**** SERIAL DEVICE ****/
226
227unsigned long serial_device_get_state(device_t *device);
228
229/* connect this device to the emulated serial chip */
230/* id is the serial device to connect to */
231/* connection is the serial connection to connect to the serial device */
232void serial_device_connect(device_t *image, serial_connection *connection);
233
234DECLARE_LEGACY_IMAGE_DEVICE(SERIAL, serial);
235
236#define MCFG_SERIAL_ADD(_tag) \
237   MCFG_DEVICE_ADD(_tag, SERIAL, 0)
238
239DEVICE_START(serial);
240DEVICE_IMAGE_LOAD(serial);
241
242void serial_device_setup(device_t *image, int baud_rate, int num_data_bits, int stop_bit_count, int parity_code);
243
244/* set the transmit state of the serial device */
245void serial_device_set_transmit_state(device_t *image, int state);
246
247#endif /* SERIAL_H_ */
src/mess/machine/msm8251.c
r0r11474
1/*********************************************************************
2
3    msm8251.c
4
5    MSM/Intel 8251 Universal Synchronous/Asynchronous Receiver Transmitter code
6
7*********************************************************************/
8
9#include "emu.h"
10#include "msm8251.h"
11
12
13/***************************************************************************
14    MACROS
15***************************************************************************/
16
17#define VERBOSE 0
18
19#define LOG(x)   do { if (VERBOSE) logerror x; } while (0)
20
21
22/***************************************************************************
23    TYPE DEFINITIONS
24***************************************************************************/
25
26typedef struct _msm8251_t msm8251_t;
27struct _msm8251_t
28{
29   devcb_resolved_read_line   in_rxd_func;
30   devcb_resolved_write_line   out_txd_func;
31   devcb_resolved_read_line   in_dsr_func;
32   devcb_resolved_write_line   out_dtr_func;
33   devcb_resolved_write_line   out_rts_func;
34   devcb_resolved_write_line   out_rxrdy_func;
35   devcb_resolved_write_line   out_txrdy_func;
36   devcb_resolved_write_line   out_txempty_func;
37   devcb_resolved_write_line   out_syndet_func;
38
39   /* flags controlling how msm8251_control_w operates */
40   UINT8 flags;
41   /* offset into sync_bytes used during sync byte transfer */
42   UINT8 sync_byte_offset;
43   /* number of sync bytes written so far */
44   UINT8 sync_byte_count;
45   /* the sync bytes written */
46   UINT8 sync_bytes[2];
47   /* status of msm8251 */
48   UINT8 status;
49   UINT8 command;
50   /* mode byte - bit definitions depend on mode - e.g. synchronous, asynchronous */
51   UINT8 mode_byte;
52
53   /* data being received */
54   UINT8 data;
55
56   /* receive reg */
57   serial_receive_register receive_reg;
58   /* transmit reg */
59   serial_transmit_register transmit_reg;
60
61   data_form form;
62
63   /* the serial connection that data is transfered over */
64   /* this is usually connected to the serial device */
65   serial_connection connection;
66};
67
68
69
70/***************************************************************************
71    PROTOTYPES
72***************************************************************************/
73
74static void msm8251_in_callback(running_machine &machine, int id, unsigned long state);
75static void msm8251_update_tx_empty(device_t *device);
76static void msm8251_update_tx_ready(device_t *device);
77
78
79
80/***************************************************************************
81    INLINE FUNCTIONS
82***************************************************************************/
83
84INLINE msm8251_t *get_token(device_t *device)
85{
86   assert(device != NULL);
87   assert(device->type() == MSM8251);
88
89   return (msm8251_t *) downcast<legacy_device_base *>(device)->token();
90}
91
92
93INLINE const msm8251_interface *get_interface(device_t *device)
94{
95   assert(device != NULL);
96   assert(device->type() == MSM8251);
97
98   return (const msm8251_interface *) device->static_config();
99}
100
101
102/***************************************************************************
103    GLOBAL VARIABLES
104***************************************************************************/
105
106const msm8251_interface default_msm8251_interface = { DEVCB_NULL, };
107
108
109/***************************************************************************
110    IMPLEMENTATION
111***************************************************************************/
112
113/*-------------------------------------------------
114    msm8251_in_callback
115-------------------------------------------------*/
116
117static void msm8251_in_callback(running_machine &machine, int id, unsigned long state)
118{
119   device_t *device;
120   msm8251_t *uart;
121   int changed;
122
123   /* NPW 29-Nov-2008 - These two lines are a hack and indicate why our "serial" infrastructure needs to be updated */
124   device = machine.device("uart");
125   uart = get_token(device);
126
127   changed = uart->connection.input_state^state;
128
129   uart->connection.input_state = state;
130
131   /* did cts change state? */
132   if (changed & SERIAL_STATE_CTS)
133   {
134      /* yes */
135      /* update tx ready */
136      /* msm8251_update_tx_ready(device); */
137   }
138}
139
140
141
142/*-------------------------------------------------
143    DEVICE_START( msm8251 )
144-------------------------------------------------*/
145
146static DEVICE_START( msm8251 )
147{
148   msm8251_t *uart = get_token(device);
149   const msm8251_interface *intf = get_interface(device);
150
151   serial_helper_setup();
152
153   // resolve callbacks
154   uart->out_rxrdy_func.resolve(intf->out_rxrdy_func, *device);
155   uart->out_txrdy_func.resolve(intf->out_txrdy_func, *device);
156   uart->out_txempty_func.resolve(intf->out_txempty_func, *device);
157
158   /* setup this side of the serial connection */
159   serial_connection_init(device->machine(),&uart->connection);
160   serial_connection_set_in_callback(device->machine(),&uart->connection, msm8251_in_callback);
161   uart->connection.input_state = 0;
162}
163
164
165
166/*-------------------------------------------------
167    msm8251_update_rx_ready
168-------------------------------------------------*/
169
170static void msm8251_update_rx_ready(device_t *device)
171{
172   msm8251_t *uart = get_token(device);
173   int state;
174
175   state = uart->status & MSM8251_STATUS_RX_READY;
176
177   /* masked? */
178   if ((uart->command & (1<<2))==0)
179   {
180      state = 0;
181   }
182
183   uart->out_rxrdy_func(state != 0);
184}
185
186
187
188/*-------------------------------------------------
189    msm8251_receive_clock
190-------------------------------------------------*/
191
192void msm8251_receive_clock(device_t *device)
193{
194   msm8251_t *uart = get_token(device);
195
196   /* receive enable? */
197   if (uart->command & (1<<2))
198   {
199      //logerror("MSM8251\n");
200      /* get bit received from other side and update receive register */
201      receive_register_update_bit(&uart->receive_reg, get_in_data_bit(uart->connection.input_state));
202
203      if (uart->receive_reg.flags & RECEIVE_REGISTER_FULL)
204      {
205         receive_register_extract(&uart->receive_reg, &uart->form);
206         msm8251_receive_character(device, uart->receive_reg.byte_received);
207      }
208   }
209}
210
211
212
213/*-------------------------------------------------
214    msm8251_transmit_clock
215-------------------------------------------------*/
216
217void msm8251_transmit_clock(device_t *device)
218{
219   msm8251_t *uart = get_token(device);
220
221   /* transmit enable? */
222   if (uart->command & (1<<0))
223   {
224
225      /* transmit register full? */
226      if ((uart->status & MSM8251_STATUS_TX_READY)==0)
227      {
228         /* if transmit reg is empty */
229         if ((uart->transmit_reg.flags & TRANSMIT_REGISTER_EMPTY)!=0)
230         {
231            /* set it up */
232            transmit_register_setup(&uart->transmit_reg, &uart->form, uart->data);
233            /* msm8251 transmit reg now empty */
234            uart->status |=MSM8251_STATUS_TX_EMPTY;
235            /* ready for next transmit */
236            uart->status |=MSM8251_STATUS_TX_READY;
237
238            msm8251_update_tx_empty(device);
239            msm8251_update_tx_ready(device);
240         }
241      }
242
243      /* if transmit is not empty... transmit data */
244      if ((uart->transmit_reg.flags & TRANSMIT_REGISTER_EMPTY)==0)
245      {
246   //      logerror("MSM8251\n");
247         transmit_register_send_bit(device->machine(),&uart->transmit_reg, &uart->connection);
248      }
249   }
250
251#if 0
252   /* hunt mode? */
253   /* after each bit has been shifted in, it is compared against the current sync byte */
254   if (uart->command & (1<<7))
255   {
256      /* data matches sync byte? */
257      if (uart->data == uart->sync_bytes[uart->sync_byte_offset])
258      {
259         /* sync byte matches */
260         /* update for next sync byte? */
261         uart->sync_byte_offset++;
262
263         /* do all sync bytes match? */
264         if (uart->sync_byte_offset == uart->sync_byte_count)
265         {
266            /* ent hunt mode */
267            uart->command &=~(1<<7);
268         }
269      }
270      else
271      {
272         /* if there is no match, reset */
273         uart->sync_byte_offset = 0;
274      }
275   }
276#endif
277}
278
279
280
281/*-------------------------------------------------
282    msm8251_update_tx_ready
283-------------------------------------------------*/
284
285static void msm8251_update_tx_ready(device_t *device)
286{
287   msm8251_t *uart = get_token(device);
288
289   /* clear tx ready state */
290   int tx_ready;
291
292   /* tx ready output is set if:
293        DB Buffer Empty &
294        CTS is set &
295        Transmit enable is 1
296    */
297
298   tx_ready = 0;
299
300   /* transmit enable? */
301   if ((uart->command & (1<<0))!=0)
302   {
303      /* other side has rts set (comes in as CTS at this side) */
304      if (uart->connection.input_state & SERIAL_STATE_CTS)
305      {
306         if (uart->status & MSM8251_STATUS_TX_EMPTY)
307         {
308            /* enable transfer */
309            tx_ready = 1;
310         }
311      }
312   }
313
314   uart->out_txrdy_func(tx_ready);
315}
316
317
318
319/*-------------------------------------------------
320    msm8251_update_tx_empty
321-------------------------------------------------*/
322
323static void msm8251_update_tx_empty(device_t *device)
324{
325   msm8251_t *uart = get_token(device);
326
327   if (uart->status & MSM8251_STATUS_TX_EMPTY)
328   {
329      /* tx is in marking state (high) when tx empty! */
330      set_out_data_bit(uart->connection.State, 1);
331      serial_connection_out(device->machine(),&uart->connection);
332   }
333
334   uart->out_txempty_func((uart->status & MSM8251_STATUS_TX_EMPTY) != 0);
335}
336
337
338
339/*-------------------------------------------------
340    DEVICE_RESET( msm8251 )
341-------------------------------------------------*/
342
343static DEVICE_RESET( msm8251 )
344{
345   msm8251_t *uart = get_token(device);
346
347   LOG(("MSM8251: Reset\n"));
348
349   /* what is the default setup when the 8251 has been reset??? */
350
351   /* msm8251 datasheet explains the state of tx pin at reset */
352   /* tx is set to 1 */
353   set_out_data_bit(uart->connection.State,1);
354
355   /* assumption, rts is set to 1 */
356   uart->connection.State &= ~SERIAL_STATE_RTS;
357   serial_connection_out(device->machine(), &uart->connection);
358
359   transmit_register_reset(&uart->transmit_reg);
360   receive_register_reset(&uart->receive_reg);
361   /* expecting mode byte */
362   uart->flags |= MSM8251_EXPECTING_MODE;
363   /* not expecting a sync byte */
364   uart->flags &= ~MSM8251_EXPECTING_SYNC_BYTE;
365
366   /* no character to read by cpu */
367   /* transmitter is ready and is empty */
368   uart->status = MSM8251_STATUS_TX_EMPTY | MSM8251_STATUS_TX_READY;
369   uart->mode_byte = 0;
370   uart->command = 0;
371
372   /* update tx empty pin output */
373   msm8251_update_tx_empty(device);
374   /* update rx ready pin output */
375   msm8251_update_rx_ready(device);
376   /* update tx ready pin output */
377   msm8251_update_tx_ready(device);
378}
379
380
381
382/*-------------------------------------------------
383    WRITE8_DEVICE_HANDLER(msm8251_control_w)
384-------------------------------------------------*/
385
386WRITE8_DEVICE_HANDLER(msm8251_control_w)
387{
388   msm8251_t *uart = get_token(device);
389
390   if (uart->flags & MSM8251_EXPECTING_MODE)
391   {
392      if (uart->flags & MSM8251_EXPECTING_SYNC_BYTE)
393      {
394         LOG(("MSM8251: Sync byte\n"));
395
396         LOG(("Sync byte: %02x\n", data));
397         /* store sync byte written */
398         uart->sync_bytes[uart->sync_byte_offset] = data;
399         uart->sync_byte_offset++;
400
401         if (uart->sync_byte_offset == uart->sync_byte_count)
402         {
403            /* finished transfering sync bytes, now expecting command */
404            uart->flags &= ~(MSM8251_EXPECTING_MODE | MSM8251_EXPECTING_SYNC_BYTE);
405            uart->sync_byte_offset = 0;
406         //  uart->status = MSM8251_STATUS_TX_EMPTY | MSM8251_STATUS_TX_READY;
407         }
408      }
409      else
410      {
411         LOG(("MSM8251: Mode byte\n"));
412
413         uart->mode_byte = data;
414
415         /* Synchronous or Asynchronous? */
416         if ((data & 0x03)!=0)
417         {
418            /*  Asynchronous
419
420                    bit 7,6: stop bit length
421                        0 = inhibit
422                        1 = 1 bit
423                        2 = 1.5 bits
424                        3 = 2 bits
425                    bit 5: parity type
426                        0 = parity odd
427                        1 = parity even
428                    bit 4: parity test enable
429                        0 = disable
430                        1 = enable
431                    bit 3,2: character length
432                        0 = 5 bits
433                        1 = 6 bits
434                        2 = 7 bits
435                        3 = 8 bits
436                    bit 1,0: baud rate factor
437                        0 = defines command byte for synchronous or asynchronous
438                        1 = x1
439                        2 = x16
440                        3 = x64
441                */
442
443            LOG(("MSM8251: Asynchronous operation\n"));
444
445            LOG(("Character length: %d\n", (((data>>2) & 0x03)+5)));
446
447            if (data & (1<<4))
448            {
449               LOG(("enable parity checking\n"));
450            }
451            else
452            {
453               LOG(("parity check disabled\n"));
454            }
455
456            if (data & (1<<5))
457            {
458               LOG(("even parity\n"));
459            }
460            else
461            {
462               LOG(("odd parity\n"));
463            }
464
465            {
466               UINT8 stop_bit_length;
467
468               stop_bit_length = (data>>6) & 0x03;
469
470               switch (stop_bit_length)
471               {
472                  case 0:
473                  {
474                     /* inhibit */
475                     LOG(("stop bit: inhibit\n"));
476                  }
477                  break;
478
479                  case 1:
480                  {
481                     /* 1 */
482                     LOG(("stop bit: 1 bit\n"));
483                  }
484                  break;
485
486                  case 2:
487                  {
488                     /* 1.5 */
489                     LOG(("stop bit: 1.5 bits\n"));
490                  }
491                  break;
492
493                  case 3:
494                  {
495                     /* 2 */
496                     LOG(("stop bit: 2 bits\n"));
497                  }
498                  break;
499               }
500            }
501
502            uart->form.word_length = ((data>>2) & 0x03)+5;
503            uart->form.parity = SERIAL_PARITY_NONE;
504            switch ((data>>6) & 0x03)
505            {
506               case 0:
507               case 1:
508                  uart->form.stop_bit_count =  1;
509                  break;
510               case 2:
511               case 3:
512                  uart->form.stop_bit_count =  2;
513                  break;
514            }
515            receive_register_setup(&uart->receive_reg, &uart->form);
516
517
518#if 0
519            /* data bits */
520            uart->receive_char_length = (((data>>2) & 0x03)+5);
521
522            if (data & (1<<4))
523            {
524               /* parity */
525               uart->receive_char_length++;
526            }
527
528            /* stop bits */
529            uart->receive_char_length++;
530
531            uart->receive_flags &=~MSM8251_TRANSFER_RECEIVE_SYNCHRONISED;
532            uart->receive_flags |= MSM8251_TRANSFER_RECEIVE_WAITING_FOR_START_BIT;
533#endif
534            /* not expecting mode byte now */
535            uart->flags &= ~MSM8251_EXPECTING_MODE;
536//              uart->status = MSM8251_STATUS_TX_EMPTY | MSM8251_STATUS_TX_READY;
537         }
538         else
539         {
540            /*  bit 7: Number of sync characters
541                        0 = 1 character
542                        1 = 2 character
543                    bit 6: Synchronous mode
544                        0 = Internal synchronisation
545                        1 = External synchronisation
546                    bit 5: parity type
547                        0 = parity odd
548                        1 = parity even
549                    bit 4: parity test enable
550                        0 = disable
551                        1 = enable
552                    bit 3,2: character length
553                        0 = 5 bits
554                        1 = 6 bits
555                        2 = 7 bits
556                        3 = 8 bits
557                    bit 1,0 = 0
558                */
559            LOG(("MSM8251: Synchronous operation\n"));
560
561            /* setup for sync byte(s) */
562            uart->flags |= MSM8251_EXPECTING_SYNC_BYTE;
563            uart->sync_byte_offset = 0;
564            if (data & 0x07)
565            {
566               uart->sync_byte_count = 1;
567            }
568            else
569            {
570               uart->sync_byte_count = 2;
571            }
572
573         }
574      }
575   }
576   else
577   {
578      /* command */
579      LOG(("MSM8251: Command byte\n"));
580
581      uart->command = data;
582
583      LOG(("Command byte: %02x\n", data));
584
585      if (data & (1<<7))
586      {
587         LOG(("hunt mode\n"));
588      }
589
590      if (data & (1<<5))
591      {
592         LOG(("/rts set to 0\n"));
593      }
594      else
595      {
596         LOG(("/rts set to 1\n"));
597      }
598
599      if (data & (1<<2))
600      {
601         LOG(("receive enable\n"));
602      }
603      else
604      {
605         LOG(("receive disable\n"));
606      }
607
608      if (data & (1<<1))
609      {
610         LOG(("/dtr set to 0\n"));
611      }
612      else
613      {
614         LOG(("/dtr set to 1\n"));
615      }
616
617      if (data & (1<<0))
618      {
619         LOG(("transmit enable\n"));
620      }
621      else
622      {
623         LOG(("transmit disable\n"));
624      }
625
626
627      /*  bit 7:
628                0 = normal operation
629                1 = hunt mode
630            bit 6:
631                0 = normal operation
632                1 = internal reset
633            bit 5:
634                0 = /RTS set to 1
635                1 = /RTS set to 0
636            bit 4:
637                0 = normal operation
638                1 = reset error flag
639            bit 3:
640                0 = normal operation
641                1 = send break character
642            bit 2:
643                0 = receive disable
644                1 = receive enable
645            bit 1:
646                0 = /DTR set to 1
647                1 = /DTR set to 0
648            bit 0:
649                0 = transmit disable
650                1 = transmit enable
651        */
652
653      uart->connection.State &=~SERIAL_STATE_RTS;
654      if (data & (1<<5))
655      {
656         /* rts set to 0 */
657         uart->connection.State |= SERIAL_STATE_RTS;
658      }
659
660      uart->connection.State &=~SERIAL_STATE_DTR;
661      if (data & (1<<1))
662      {
663         uart->connection.State |= SERIAL_STATE_DTR;
664      }
665
666      if ((data & (1<<0))==0)
667      {
668         /* held in high state when transmit disable */
669         set_out_data_bit(uart->connection.State,1);
670      }
671
672
673      /* refresh outputs */
674      serial_connection_out(device->machine(), &uart->connection);
675
676      if (data & (1<<4))
677      {
678         uart->status &= ~(MSM8251_STATUS_PARITY_ERROR | MSM8251_STATUS_OVERRUN_ERROR | MSM8251_STATUS_FRAMING_ERROR);
679      }
680
681      if (data & (1<<6))
682      {
683         device->reset();
684      }
685
686      msm8251_update_rx_ready(device);
687      msm8251_update_tx_ready(device);
688
689   }
690}
691
692
693
694/*-------------------------------------------------
695    READ8_DEVICE_HANDLER(msm8251_status_r)
696-------------------------------------------------*/
697
698READ8_DEVICE_HANDLER(msm8251_status_r)
699{
700   msm8251_t *uart = get_token(device);
701
702   LOG(("status: %02x\n", uart->status));
703   return uart->status;
704}
705
706
707
708/*-------------------------------------------------
709    WRITE8_DEVICE_HANDLER(msm8251_data_w)
710-------------------------------------------------*/
711
712WRITE8_DEVICE_HANDLER(msm8251_data_w)
713{
714   msm8251_t *uart = get_token(device);
715
716   uart->data = data;
717
718   logerror("write data: %02x\n",data);
719
720   /* writing clears */
721   uart->status &=~MSM8251_STATUS_TX_READY;
722
723   /* if transmitter is active, then tx empty will be signalled */
724
725   msm8251_update_tx_ready(device);
726}
727
728
729
730/*-------------------------------------------------
731    msm8251_receive_character - called when last
732    bit of data has been received
733-------------------------------------------------*/
734
735void msm8251_receive_character(device_t *device, UINT8 ch)
736{
737   msm8251_t *uart = get_token(device);
738
739   logerror("msm8251 receive char: %02x\n",ch);
740
741   uart->data = ch;
742
743   /* char has not been read and another has arrived! */
744   if (uart->status & MSM8251_STATUS_RX_READY)
745   {
746      uart->status |= MSM8251_STATUS_OVERRUN_ERROR;
747   }
748   uart->status |= MSM8251_STATUS_RX_READY;
749
750   msm8251_update_rx_ready(device);
751}
752
753
754
755/*-------------------------------------------------
756    READ8_DEVICE_HANDLER(msm8251_data_r) - read data
757-------------------------------------------------*/
758
759READ8_DEVICE_HANDLER(msm8251_data_r)
760{
761   msm8251_t *uart = get_token(device);
762
763   logerror("read data: %02x, STATUS=%02x\n",uart->data,uart->status);
764   /* reading clears */
765   uart->status &= ~MSM8251_STATUS_RX_READY;
766
767   msm8251_update_rx_ready(device);
768   return uart->data;
769}
770
771
772
773/*-------------------------------------------------
774    msm8251_connect_to_serial_device - initialise
775    transfer using serial device - set the callback
776    which will be called when serial device has
777    updated it's state
778-------------------------------------------------*/
779
780void msm8251_connect_to_serial_device(device_t *device, device_t *image)
781{
782   msm8251_t *uart = get_token(device);
783   serial_device_connect(image, &uart->connection);
784}
785
786
787
788/*-------------------------------------------------
789    msm8251_connect
790-------------------------------------------------*/
791
792void msm8251_connect(device_t *device, serial_connection *other_connection)
793{
794   msm8251_t *uart = get_token(device);
795   serial_connection_link(device->machine(), &uart->connection, other_connection);
796}
797
798
799/*-------------------------------------------------
800    DEVICE_GET_INFO( msm8251 )
801-------------------------------------------------*/
802
803DEVICE_GET_INFO( msm8251 )
804{
805   switch (state)
806   {
807      /* --- the following bits of info are returned as 64-bit signed integers --- */
808      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(msm8251_t);            break;
809      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
810
811      /* --- the following bits of info are returned as pointers to data or functions --- */
812      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(msm8251);   break;
813      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
814      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(msm8251);   break;
815
816      /* --- the following bits of info are returned as NULL-terminated strings --- */
817      case DEVINFO_STR_NAME:                     strcpy(info->s, "Intel 8251 UART");            break;
818      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "Intel 8251 UART");            break;
819      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                     break;
820      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                     break;
821      case DEVINFO_STR_CREDITS:                  /* Nothing */                        break;
822   }
823}
824
825DEFINE_LEGACY_DEVICE(MSM8251, msm8251);
src/mess/machine/msm8251.h
r0r11474
1/*********************************************************************
2
3    msm8251.h
4
5    MSM/Intel 8251 Universal Synchronous/Asynchronous Receiver Transmitter code
6
7*********************************************************************/
8
9#ifndef __MSM8251_H__
10#define __MSM8251_H__
11
12#include "machine/serial.h"
13
14
15/***************************************************************************
16    CONSTANTS
17***************************************************************************/
18
19DECLARE_LEGACY_DEVICE(MSM8251, msm8251);
20
21#define MSM8251_EXPECTING_MODE 0x01
22#define MSM8251_EXPECTING_SYNC_BYTE 0x02
23
24#define MSM8251_STATUS_FRAMING_ERROR 0x020
25#define MSM8251_STATUS_OVERRUN_ERROR 0x010
26#define MSM8251_STATUS_PARITY_ERROR 0x08
27#define MSM8251_STATUS_TX_EMPTY      0x04
28#define MSM8251_STATUS_RX_READY   0x02
29#define MSM8251_STATUS_TX_READY   0x01
30
31#define MCFG_MSM8251_ADD(_tag, _intrf) \
32   MCFG_DEVICE_ADD(_tag, MSM8251, 0) \
33   MCFG_DEVICE_CONFIG(_intrf)
34
35#define MCFG_MSM8251_REMOVE(_tag) \
36   MCFG_DEVICE_REMOVE(_tag)
37
38
39/***************************************************************************
40    TYPE DEFINITIONS
41***************************************************************************/
42
43typedef struct _msm8251_interface msm8251_interface;
44struct _msm8251_interface
45{
46   devcb_read_line      in_rxd_func;
47   devcb_write_line   out_txd_func;
48   devcb_read_line      in_dsr_func;
49   devcb_write_line   out_dtr_func;
50   devcb_write_line   out_rts_func;
51   devcb_write_line   out_rxrdy_func;
52   devcb_write_line   out_txrdy_func;
53   devcb_write_line   out_txempty_func;
54   devcb_write_line   out_syndet_func;
55};
56
57
58/***************************************************************************
59    PROTOTYPES
60***************************************************************************/
61
62extern const msm8251_interface default_msm8251_interface;
63
64/* read data register */
65READ8_DEVICE_HANDLER(msm8251_data_r);
66
67/* read status register */
68READ8_DEVICE_HANDLER(msm8251_status_r);
69
70/* write data register */
71WRITE8_DEVICE_HANDLER(msm8251_data_w);
72
73/* write control word */
74WRITE8_DEVICE_HANDLER(msm8251_control_w);
75
76/* The 8251 has seperate transmit and receive clocks */
77/* use these two functions to update the msm8251 for each clock */
78/* on NC100 system, the clocks are the same */
79void msm8251_transmit_clock(device_t *device);
80void msm8251_receive_clock(device_t *device);
81
82/* connecting to serial output */
83void msm8251_connect_to_serial_device(device_t *device, device_t *image);
84void msm8251_connect(device_t *device, serial_connection *other_connection);
85
86void msm8251_receive_character(device_t *device, UINT8 ch);
87
88#endif /* __MSM8251_H__ */
src/mess/drivers/sg1000.c
r11473r11474
7777#include "machine/msm8251.h"
7878#include "machine/upd765.h"
7979#include "sound/sn76496.h"
80#include "video/tms9928a.h"
8180#include "crsshair.h"
8281#include "includes/sg1000.h"
8382
84/***************************************************************************
85    READ/WRITE HANDLERS
86***************************************************************************/
87
88/*
89
90    Terebi Oekaki (TV Draw)
91
92    Address Access  Bits
93                    7       6   5   4   3   2   1   0
94    $6000   W       -       -   -   -   -   -   -   AXIS
95    $8000   R       BUSY    -   -   -   -   -   -   PRESS
96    $A000   R/W     DATA
97
98    AXIS: write 0 to select X axis, 1 to select Y axis.
99    BUSY: reads 1 when graphic board is busy sampling position, else 0.
100    PRESS: reads 0 when pen is touching graphic board, else 1.
101    DATA: when pen is touching graphic board, return 8-bit sample position for currently selected axis (X is in the 0-255 range, Y in the 0-191 range). Else, return 0.
102
103*/
104
105/*-------------------------------------------------
106    tvdraw_axis_w - TV Draw axis select
107-------------------------------------------------*/
108
109WRITE8_MEMBER( sg1000_state::tvdraw_axis_w )
83class sf7000_state : public sc3000_state
11084{
111   if (data & 0x01)
112   {
113      m_tvdraw_data = input_port_read(machine(), "TVDRAW_X");
85public:
86   sf7000_state(const machine_config &mconfig, device_type type, const char *tag)
87      : sc3000_state(mconfig, type, tag),
88        m_fdc(*this, UPD765_TAG),
89        m_centronics(*this, CENTRONICS_TAG),
90        m_floppy0(*this, FLOPPY_0)
91   { }
11492
115      if (m_tvdraw_data < 4) m_tvdraw_data = 4;
116      if (m_tvdraw_data > 251) m_tvdraw_data = 251;
117   }
118   else
119   {
120      m_tvdraw_data = input_port_read(machine(), "TVDRAW_Y") + 32;
121   }
122}
93   required_device<device_t> m_fdc;
94   required_device<device_t> m_centronics;
95   required_device<device_t> m_floppy0;
12396
124/*-------------------------------------------------
125    tvdraw_status_r - TV Draw status read
126-------------------------------------------------*/
97   virtual void machine_start();
98   virtual void machine_reset();
12799
128READ8_MEMBER( sg1000_state::tvdraw_status_r )
129{
130   return input_port_read(machine(), "TVDRAW_PEN");
131}
100   DECLARE_READ8_MEMBER( ppi_pa_r );
101   DECLARE_WRITE8_MEMBER( ppi_pc_w );
102   DECLARE_WRITE_LINE_MEMBER( fdc_intrq_w );
132103
133/*-------------------------------------------------
134    tvdraw_data_r - TV Draw data read
135-------------------------------------------------*/
104   /* floppy state */
105   int m_fdc_irq;
106   int m_fdc_index;
107};
136108
137READ8_MEMBER( sg1000_state::tvdraw_data_r )
138{
139   return m_tvdraw_data;
140}
141
142109/*-------------------------------------------------
143    joysel_r -
144-------------------------------------------------*/
145
146READ8_MEMBER( sg1000_state::joysel_r )
147{
148   return 0x80;
149}
150
151/***************************************************************************
152    MEMORY MAPS
153***************************************************************************/
154
155/*-------------------------------------------------
156    ADDRESS_MAP( sg1000_map )
157-------------------------------------------------*/
158
159static ADDRESS_MAP_START( sg1000_map, AS_PROGRAM, 8, sg1000_state )
160   AM_RANGE(0x0000, 0x7fff) AM_ROM
161   AM_RANGE(0x8000, 0xbfff) AM_RAMBANK("bank1")
162   AM_RANGE(0xc000, 0xffff) AM_RAMBANK("bank2")
163ADDRESS_MAP_END
164
165/*-------------------------------------------------
166    ADDRESS_MAP( sg1000_io_map )
167-------------------------------------------------*/
168
169static ADDRESS_MAP_START( sg1000_io_map, AS_IO, 8, sg1000_state )
170   ADDRESS_MAP_GLOBAL_MASK(0xff)
171   AM_RANGE(0x40, 0x40) AM_MIRROR(0x3f) AM_DEVWRITE_LEGACY(SN76489A_TAG, sn76496_w)
172   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_READWRITE_LEGACY(TMS9928A_vram_r, TMS9928A_vram_w)
173   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_READWRITE_LEGACY(TMS9928A_register_r, TMS9928A_register_w)
174   AM_RANGE(0xdc, 0xdc) AM_READ_PORT("PA7")
175   AM_RANGE(0xdd, 0xdd) AM_READ_PORT("PB7")
176   AM_RANGE(0xde, 0xde) AM_READ(joysel_r) AM_WRITENOP
177   AM_RANGE(0xdf, 0xdf) AM_NOP
178ADDRESS_MAP_END
179
180/*-------------------------------------------------
181    ADDRESS_MAP( omv_map )
182-------------------------------------------------*/
183
184static ADDRESS_MAP_START( omv_map, AS_PROGRAM, 8, sg1000_state )
185   AM_RANGE(0x0000, 0x7fff) AM_ROM
186   AM_RANGE(0x8000, 0xbfff) AM_RAMBANK("bank1")
187   AM_RANGE(0xc000, 0xc7ff) AM_MIRROR(0x3800) AM_RAM
188ADDRESS_MAP_END
189
190/*-------------------------------------------------
191    ADDRESS_MAP( omv_io_map )
192-------------------------------------------------*/
193
194static ADDRESS_MAP_START( omv_io_map, AS_IO, 8, sg1000_state )
195   ADDRESS_MAP_GLOBAL_MASK(0xff)
196   AM_RANGE(0x40, 0x40) AM_MIRROR(0x3f) AM_DEVWRITE_LEGACY(SN76489A_TAG, sn76496_w)
197   AM_RANGE(0x80, 0x80) AM_MIRROR(0x3e) AM_READWRITE_LEGACY(TMS9928A_vram_r, TMS9928A_vram_w)
198   AM_RANGE(0x81, 0x81) AM_MIRROR(0x3e) AM_READWRITE_LEGACY(TMS9928A_register_r, TMS9928A_register_w)
199   AM_RANGE(0xc0, 0xc0) AM_MIRROR(0x38) AM_READ_PORT("C0")
200   AM_RANGE(0xc1, 0xc1) AM_MIRROR(0x38) AM_READ_PORT("C1")
201   AM_RANGE(0xc2, 0xc2) AM_MIRROR(0x38) AM_READ_PORT("C2")
202   AM_RANGE(0xc3, 0xc3) AM_MIRROR(0x38) AM_READ_PORT("C3")
203   AM_RANGE(0xc4, 0xc4) AM_MIRROR(0x3a) AM_READ_PORT("C4")
204   AM_RANGE(0xc5, 0xc5) AM_MIRROR(0x3a) AM_READ_PORT("C5")
205ADDRESS_MAP_END
206
207/*-------------------------------------------------
208    ADDRESS_MAP( sc3000_map )
209-------------------------------------------------*/
210
211static ADDRESS_MAP_START( sc3000_map, AS_PROGRAM, 8, sg1000_state )
212   AM_RANGE(0x0000, 0x7fff) AM_ROM
213   AM_RANGE(0x8000, 0xbfff) AM_RAMBANK("bank1")
214   AM_RANGE(0xc000, 0xffff) AM_RAMBANK("bank2")
215ADDRESS_MAP_END
216
217/*-------------------------------------------------
218    ADDRESS_MAP( sc3000_io_map )
219-------------------------------------------------*/
220
221static ADDRESS_MAP_START( sc3000_io_map, AS_IO, 8, sg1000_state )
222   ADDRESS_MAP_GLOBAL_MASK(0xff)
223   AM_RANGE(0x7f, 0x7f) AM_DEVWRITE_LEGACY(SN76489A_TAG, sn76496_w)
224   AM_RANGE(0xbe, 0xbe) AM_READWRITE_LEGACY(TMS9928A_vram_r, TMS9928A_vram_w)
225   AM_RANGE(0xbf, 0xbf) AM_READWRITE_LEGACY(TMS9928A_register_r, TMS9928A_register_w)
226   AM_RANGE(0xdc, 0xdf) AM_DEVREADWRITE(UPD9255_TAG, i8255_device, read, write)
227ADDRESS_MAP_END
228
229/* This is how the I/O ports are really mapped, but MAME does not support overlapping ranges
230static ADDRESS_MAP_START( sc3000_io_map, AS_IO, 8, sg1000_state )
231    ADDRESS_MAP_GLOBAL_MASK(0xff)
232    AM_RANGE(0x00, 0x00) AM_MIRROR(0xdf) AM_DEVREADWRITE(UPD9255_TAG, i8255_device, read, write)
233    AM_RANGE(0x00, 0x00) AM_MIRROR(0x7f) AM_DEVWRITE_LEGACY(SN76489A_TAG, sn76496_w)
234    AM_RANGE(0x00, 0x00) AM_MIRROR(0xae) AM_READWRITE_LEGACY(TMS9928A_vram_r, TMS9928A_vram_w)
235    AM_RANGE(0x01, 0x01) AM_MIRROR(0xae) AM_READWRITE_LEGACY(TMS9928A_register_r, TMS9928A_register_w)
236    AM_RANGE(0x60, 0x60) AM_MIRROR(0x9f) AM_READ(sc3000_r_r)
237ADDRESS_MAP_END
238*/
239
240/*-------------------------------------------------
241110    ADDRESS_MAP( sf7000_map )
242111-------------------------------------------------*/
243112
r11473r11474
268137***************************************************************************/
269138
270139/*-------------------------------------------------
271    INPUT_CHANGED( trigger_nmi )
272-------------------------------------------------*/
273
274static INPUT_CHANGED( trigger_nmi )
275{
276   cputag_set_input_line(field->port->machine(), Z80_TAG, INPUT_LINE_NMI, (input_port_read(field->port->machine(), "NMI") ? CLEAR_LINE : ASSERT_LINE));
277}
278
279/*-------------------------------------------------
280    INPUT_PORTS( tvdraw )
281-------------------------------------------------*/
282
283static INPUT_PORTS_START( tvdraw )
284   PORT_START("TVDRAW_X")
285   PORT_BIT( 0xff, 0x80, IPT_LIGHTGUN_X ) PORT_CROSSHAIR(X, 1.0, 0.0, 0) PORT_SENSITIVITY(50) PORT_KEYDELTA(10) PORT_PLAYER(1)
286
287   PORT_START("TVDRAW_Y")
288   PORT_BIT( 0xff, 0x60, IPT_LIGHTGUN_Y ) PORT_CROSSHAIR(Y, 1.0, 0.0, 0) PORT_MINMAX(0, 191) PORT_SENSITIVITY(50) PORT_KEYDELTA(10) PORT_PLAYER(1)
289
290   PORT_START("TVDRAW_PEN")
291   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_NAME("Pen")
292INPUT_PORTS_END
293
294/*-------------------------------------------------
295    INPUT_PORTS( sg1000 )
296-------------------------------------------------*/
297
298static INPUT_PORTS_START( sg1000 )
299   PORT_START("PA7")
300   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_UP )
301   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN )
302   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT )
303   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT )
304   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON1 )
305   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON2 )
306   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_PLAYER(2)
307   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_PLAYER(2)
308
309   PORT_START("PB7")
310   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_PLAYER(2)
311   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(2)
312   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2)
313   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2)
314   PORT_BIT( 0xf0, IP_ACTIVE_LOW, IPT_UNUSED )
315
316   PORT_START("NMI")
317   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_START ) PORT_NAME("PAUSE") PORT_CODE(KEYCODE_P) PORT_CHANGED(trigger_nmi, 0)
318
319   PORT_INCLUDE( tvdraw )
320INPUT_PORTS_END
321
322/*-------------------------------------------------
323    INPUT_PORTS( omv )
324-------------------------------------------------*/
325
326static INPUT_PORTS_START( omv )
327   PORT_START("C0")
328    PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_1) PORT_CHAR('1')
329    PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_2) PORT_CHAR('2')
330    PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_3) PORT_CHAR('3')
331    PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_4) PORT_CHAR('4')
332    PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_5) PORT_CHAR('5')
333    PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_6) PORT_CHAR('6')
334   PORT_BIT( 0xc0, IP_ACTIVE_LOW, IPT_UNUSED )
335
336   PORT_START("C1")
337    PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_7) PORT_CHAR('7')
338   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_8) PORT_CHAR('8')
339   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_NAME("9 #") PORT_CODE(KEYCODE_9) PORT_CHAR('9')
340   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_NAME("0 *") PORT_CODE(KEYCODE_0) PORT_CHAR('0')
341   PORT_BIT( 0xf0, IP_ACTIVE_LOW, IPT_UNUSED )
342
343   PORT_START("C2")
344   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_A) PORT_CHAR('A')
345    PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_B) PORT_CHAR('B')
346    PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_C) PORT_CHAR('C')
347    PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_D) PORT_CHAR('D')
348    PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_E) PORT_CHAR('E')
349    PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_F) PORT_CHAR('F')
350   PORT_BIT( 0xc0, IP_ACTIVE_LOW, IPT_UNUSED )
351
352   PORT_START("C3")
353   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_G) PORT_CHAR('G')
354    PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYPAD ) PORT_CODE(KEYCODE_H) PORT_CHAR('H')
355   PORT_BIT( 0xfc, IP_ACTIVE_LOW, IPT_UNUSED )
356
357   PORT_START("C4")
358   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_UP )
359   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN )
360   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT )
361   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT )
362   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_NAME("S-1")
363   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_NAME("S-2")
364   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_PLAYER(2)
365   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_PLAYER(2)
366
367   PORT_START("C5")
368   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_PLAYER(2)
369   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(2)
370   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2)
371   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2)
372   PORT_BIT( 0xf0, IP_ACTIVE_LOW, IPT_UNUSED )
373
374   PORT_INCLUDE( tvdraw )
375INPUT_PORTS_END
376
377/*-------------------------------------------------
378    INPUT_PORTS( sk1100 )
379-------------------------------------------------*/
380
381static INPUT_PORTS_START( sk1100 )
382   PORT_START("PA0")
383   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_1) PORT_CHAR('1') PORT_CHAR('!')
384   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_Q) PORT_CHAR('Q') PORT_CHAR('q')
385   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_A) PORT_CHAR('A') PORT_CHAR('a')
386   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_Z) PORT_CHAR('Z') PORT_CHAR('z')
387   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("ENG DIER'S") PORT_CODE(KEYCODE_RALT) PORT_CHAR(UCHAR_MAMEKEY(RALT))
388   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_COMMA) PORT_CHAR(',') PORT_CHAR('<')
389   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_K) PORT_CHAR('K') PORT_CHAR('k')
390   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_I) PORT_CHAR('I') PORT_CHAR('i')
391
392   PORT_START("PA1")
393   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_2) PORT_CHAR('2') PORT_CHAR('"')
394   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_W) PORT_CHAR('W') PORT_CHAR('w')
395   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_S) PORT_CHAR('S') PORT_CHAR('s')
396   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_X) PORT_CHAR('X') PORT_CHAR('x')
397   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("SPC") PORT_CODE(KEYCODE_SPACE) PORT_CHAR(' ')
398   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_STOP) PORT_CHAR('.') PORT_CHAR('>')
399   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_L) PORT_CHAR('L') PORT_CHAR('l')
400   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_O) PORT_CHAR('O') PORT_CHAR('o')
401
402   PORT_START("PA2")
403   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_3) PORT_CHAR('3') PORT_CHAR('#')
404   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_E) PORT_CHAR('E') PORT_CHAR('e')
405   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_D) PORT_CHAR('D') PORT_CHAR('d')
406   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_C) PORT_CHAR('C') PORT_CHAR('c')
407   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("HOME CLR") PORT_CODE(KEYCODE_HOME) PORT_CHAR(UCHAR_MAMEKEY(HOME))
408   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_SLASH) PORT_CHAR('/') PORT_CHAR('?')
409   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_COLON) PORT_CHAR(';') PORT_CHAR('+')
410   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_P) PORT_CHAR('P') PORT_CHAR('p')
411
412   PORT_START("PA3")
413   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_4) PORT_CHAR('4') PORT_CHAR('$')
414   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_R) PORT_CHAR('R') PORT_CHAR('r')
415   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_F) PORT_CHAR('F') PORT_CHAR('f')
416   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_V) PORT_CHAR('V') PORT_CHAR('v')
417   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("INS DEL") PORT_CODE(KEYCODE_BACKSPACE) PORT_CHAR(8)
418   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("\xcf\x80") PORT_CODE(KEYCODE_EQUALS) PORT_CHAR(0x03c0)
419   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_QUOTE) PORT_CHAR(':') PORT_CHAR('*')
420   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_OPENBRACE) PORT_CHAR('@') PORT_CHAR('`')
421
422   PORT_START("PA4")
423   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_5) PORT_CHAR('5') PORT_CHAR('%')
424   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_T) PORT_CHAR('T') PORT_CHAR('t')
425   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_G) PORT_CHAR('G') PORT_CHAR('g')
426   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_B) PORT_CHAR('B') PORT_CHAR('b')
427   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNUSED )
428   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME(UTF8_DOWN) PORT_CODE(KEYCODE_DOWN) PORT_CHAR(UCHAR_MAMEKEY(DOWN))
429   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_BACKSLASH) PORT_CHAR(']') PORT_CHAR('}')
430   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_CLOSEBRACE) PORT_CHAR('[') PORT_CHAR('{')
431
432   PORT_START("PA5")
433   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_6) PORT_CHAR('6') PORT_CHAR('&')
434   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_Y) PORT_CHAR('Y') PORT_CHAR('y')
435   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_H) PORT_CHAR('H') PORT_CHAR('h')
436   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_N) PORT_CHAR('N') PORT_CHAR('n')
437   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNUSED )
438   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME(UTF8_LEFT) PORT_CODE(KEYCODE_LEFT) PORT_CHAR(UCHAR_MAMEKEY(LEFT))
439   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("CR") PORT_CODE(KEYCODE_ENTER) PORT_CHAR(13)
440   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNUSED )
441
442   PORT_START("PA6")
443   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_7) PORT_CHAR('7') PORT_CHAR('\'')
444   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_U) PORT_CHAR('U') PORT_CHAR('u')
445   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_J) PORT_CHAR('J') PORT_CHAR('j')
446   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_M) PORT_CHAR('M') PORT_CHAR('m')
447   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_UNUSED )
448   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME(UTF8_RIGHT) PORT_CODE(KEYCODE_RIGHT) PORT_CHAR(UCHAR_MAMEKEY(RIGHT))
449   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME(UTF8_UP) PORT_CODE(KEYCODE_UP) PORT_CHAR(UCHAR_MAMEKEY(UP))
450   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNUSED )
451
452   PORT_START("PA7")
453   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_UP )
454   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN )
455   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT )
456   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT )
457   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON1 )
458   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON2 )
459   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_JOYSTICK_UP ) PORT_PLAYER(2)
460   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN ) PORT_PLAYER(2)
461
462   PORT_START("PB0")
463   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_8) PORT_CHAR('8') PORT_CHAR('(')
464   PORT_BIT( 0x06, IP_ACTIVE_LOW, IPT_UNUSED )
465
466   PORT_START("PB1")
467   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_9) PORT_CHAR('9') PORT_CHAR(')')
468   PORT_BIT( 0x06, IP_ACTIVE_LOW, IPT_UNUSED )
469
470   PORT_START("PB2")
471   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_0) PORT_CHAR('0')
472   PORT_BIT( 0x06, IP_ACTIVE_LOW, IPT_UNUSED )
473
474   PORT_START("PB3")
475   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_MINUS) PORT_CHAR('-') PORT_CHAR('=')
476   PORT_BIT( 0x06, IP_ACTIVE_LOW, IPT_UNUSED )
477
478   PORT_START("PB4")
479   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_CODE(KEYCODE_BACKSLASH2) PORT_CHAR('^')
480   PORT_BIT( 0x06, IP_ACTIVE_LOW, IPT_UNUSED )
481
482   PORT_START("PB5")
483   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("\xc2\xa5") PORT_CODE(KEYCODE_TILDE) PORT_CHAR(0x00a5)
484   PORT_BIT( 0x06, IP_ACTIVE_LOW, IPT_UNUSED )
485   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("FUNC") PORT_CODE(KEYCODE_TAB)
486
487   PORT_START("PB6")
488   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("BREAK") PORT_CODE(KEYCODE_ESC) PORT_CHAR(UCHAR_MAMEKEY(ESC))
489   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("GRAPH") PORT_CODE(KEYCODE_LALT) PORT_CHAR(UCHAR_MAMEKEY(LALT))
490   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("CTRL") PORT_CODE(KEYCODE_LCONTROL) PORT_CHAR(UCHAR_MAMEKEY(LCONTROL))
491   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("SHIFT") PORT_CODE(KEYCODE_LSHIFT) PORT_CODE(KEYCODE_RSHIFT) PORT_CHAR(UCHAR_SHIFT_1)
492
493   PORT_START("PB7")
494   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT ) PORT_PLAYER(2)
495   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(2)
496   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2)
497   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2)
498
499   PORT_START("NMI")
500   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_KEYBOARD ) PORT_NAME("RESET") PORT_CODE(KEYCODE_F10) PORT_CHANGED(trigger_nmi, 0)
501INPUT_PORTS_END
502
503/*-------------------------------------------------
504    INPUT_PORTS( sc3000 )
505-------------------------------------------------*/
506
507static INPUT_PORTS_START( sc3000 )
508   PORT_INCLUDE( sk1100 )
509   PORT_INCLUDE( tvdraw )
510INPUT_PORTS_END
511
512/*-------------------------------------------------
513140    INPUT_PORTS( sf7000 )
514141-------------------------------------------------*/
515142
r11473r11474
531158***************************************************************************/
532159
533160/*-------------------------------------------------
534    TMS9928a_interface tms9928a_interface
535-------------------------------------------------*/
536
537static INTERRUPT_GEN( sg1000_int )
538{
539    TMS9928A_interrupt(device->machine());
540}
541
542static void sg1000_vdp_interrupt(running_machine &machine, int state)
543{
544   cputag_set_input_line(machine, Z80_TAG, INPUT_LINE_IRQ0, state);
545}
546
547static const TMS9928a_interface tms9928a_interface =
548{
549   TMS99x8A,
550   0x4000,
551   0, 0,
552   sg1000_vdp_interrupt
553};
554
555/*-------------------------------------------------
556    I8255_INTERFACE( sc3000_ppi_intf )
557-------------------------------------------------*/
558
559READ8_MEMBER( sc3000_state::ppi_pa_r )
560{
561   /*
562        Signal  Description
563
564        PA0     Keyboard input
565        PA1     Keyboard input
566        PA2     Keyboard input
567        PA3     Keyboard input
568        PA4     Keyboard input
569        PA5     Keyboard input
570        PA6     Keyboard input
571        PA7     Keyboard input
572    */
573
574   static const char *const keynames[] = { "PA0", "PA1", "PA2", "PA3", "PA4", "PA5", "PA6", "PA7" };
575
576   return input_port_read(machine(), keynames[m_keylatch]);
577}
578
579READ8_MEMBER( sc3000_state::ppi_pb_r )
580{
581   /*
582        Signal  Description
583
584        PB0     Keyboard input
585        PB1     Keyboard input
586        PB2     Keyboard input
587        PB3     Keyboard input
588        PB4     /CONT input from cartridge terminal B-11
589        PB5     FAULT input from printer
590        PB6     BUSY input from printer
591        PB7     Cassette tape input
592    */
593
594   static const char *const keynames[] = { "PB0", "PB1", "PB2", "PB3", "PB4", "PB5", "PB6", "PB7" };
595
596   /* keyboard */
597   UINT8 data = input_port_read(machine(), keynames[m_keylatch]);
598
599   /* cartridge contact */
600   data |= 0x10;
601
602   /* printer */
603   data |= 0x60;
604
605   /* tape input */
606   if (cassette_input(m_cassette) > +0.0) data |= 0x80;
607
608   return data;
609}
610
611WRITE8_MEMBER( sc3000_state::ppi_pc_w )
612{
613   /*
614        Signal  Description
615
616        PC0     Keyboard raster output
617        PC1     Keyboard raster output
618        PC2     Keyboard raster output
619        PC3     not connected
620        PC4     Cassette tape output
621        PC5     DATA to printer
622        PC6     /RESET to printer
623        PC7     /FEED to printer
624    */
625
626   /* keyboard */
627   m_keylatch = data & 0x07;
628
629   /* cassette */
630   cassette_output(m_cassette, BIT(data, 4) ? +1.0 : -1.0);
631
632   /* TODO printer */
633}
634
635static I8255_INTERFACE( sc3000_ppi_intf )
636{
637   DEVCB_DRIVER_MEMBER(sc3000_state, ppi_pa_r),   // Port A read
638   DEVCB_NULL,                              // Port A write
639   DEVCB_DRIVER_MEMBER(sc3000_state, ppi_pb_r),   // Port B read
640   DEVCB_NULL,                              // Port B write
641   DEVCB_NULL,                              // Port C read
642   DEVCB_DRIVER_MEMBER(sc3000_state, ppi_pc_w),   // Port C write
643};
644
645/*-------------------------------------------------
646    cassette_config sc3000_cassette_config
647-------------------------------------------------*/
648
649static const cassette_config sc3000_cassette_config =
650{
651   cassette_default_formats,
652   NULL,
653   (cassette_state)(CASSETTE_STOPPED | CASSETTE_MOTOR_ENABLED | CASSETTE_SPEAKER_ENABLED),
654   NULL
655};
656
657/*-------------------------------------------------
658161    I8255_INTERFACE( sf7000_ppi_intf )
659162-------------------------------------------------*/
660163
r11473r11474
775278};
776279
777280/***************************************************************************
778    CARTRIDGE LOADING
779***************************************************************************/
780
781/*-------------------------------------------------
782    sg1000_install_cartridge -
783-------------------------------------------------*/
784
785void sg1000_state::install_cartridge(UINT8 *ptr, int size)
786{
787   address_space *program = m_maincpu->memory().space(AS_PROGRAM);
788
789   switch (size)
790   {
791   case 40 * 1024:
792      program->install_read_bank(0x8000, 0x9fff, "bank1");
793      program->unmap_write(0x8000, 0x9fff);
794      memory_configure_bank(machine(), "bank1", 0, 1, machine().region(Z80_TAG)->base() + 0x8000, 0);
795      memory_set_bank(machine(), "bank1", 0);
796      break;
797
798   case 48 * 1024:
799      program->install_read_bank(0x8000, 0xbfff, "bank1");
800      program->unmap_write(0x8000, 0xbfff);
801      memory_configure_bank(machine(), "bank1", 0, 1, machine().region(Z80_TAG)->base() + 0x8000, 0);
802      memory_set_bank(machine(), "bank1", 0);
803      break;
804
805   default:
806      if (IS_CARTRIDGE_TV_DRAW(ptr))
807      {
808         program->install_write_handler(0x6000, 0x6000, 0, 0, write8_delegate(FUNC(sg1000_state::tvdraw_axis_w), this), 0);
809         program->install_read_handler(0x8000, 0x8000, 0, 0, read8_delegate(FUNC(sg1000_state::tvdraw_status_r), this), 0);
810         program->install_read_handler(0xa000, 0xa000, 0, 0, read8_delegate(FUNC(sg1000_state::tvdraw_data_r), this), 0);
811         program->nop_write(0xa000, 0xa000);
812      }
813      else if (IS_CARTRIDGE_THE_CASTLE(ptr))
814      {
815         program->install_readwrite_bank(0x8000, 0x9fff, "bank1");
816      }
817      break;
818   }
819}
820
821/*-------------------------------------------------
822    DEVICE_IMAGE_LOAD( sg1000_cart )
823-------------------------------------------------*/
824
825static DEVICE_IMAGE_LOAD( sg1000_cart )
826{
827   running_machine &machine = image.device().machine();
828   sg1000_state *state = machine.driver_data<sg1000_state>();
829   address_space *program = machine.device(Z80_TAG)->memory().space(AS_PROGRAM);
830   UINT8 *ptr = machine.region(Z80_TAG)->base();
831   UINT32 size;
832
833   if (image.software_entry() == NULL)
834   {
835      size = image.length();
836      if (image.fread( ptr, size) != size)
837         return IMAGE_INIT_FAIL;
838   }
839   else
840   {
841      size = image.get_software_region_length("rom");
842      memcpy(ptr, image.get_software_region("rom"), size);
843   }
844
845   /* cartridge ROM banking */
846   state->install_cartridge(ptr, size);
847
848   /* work RAM banking */
849   program->install_readwrite_bank(0xc000, 0xc3ff, 0, 0x3c00, "bank2");
850
851   return IMAGE_INIT_PASS;
852}
853
854/*-------------------------------------------------
855    DEVICE_IMAGE_LOAD( omv_cart )
856-------------------------------------------------*/
857
858static DEVICE_IMAGE_LOAD( omv_cart )
859{
860   running_machine &machine = image.device().machine();
861   sg1000_state *state = machine.driver_data<sg1000_state>();
862   UINT32 size;
863   UINT8 *ptr = machine.region(Z80_TAG)->base();
864
865   if (image.software_entry() == NULL)
866   {
867      size = image.length();
868      if (image.fread( ptr, size) != size)
869         return IMAGE_INIT_FAIL;
870   }
871   else
872   {
873      size = image.get_software_region_length("rom");
874      memcpy(ptr, image.get_software_region("rom"), size);
875   }
876
877   /* cartridge ROM banking */
878   state->install_cartridge(ptr, size);
879
880   return IMAGE_INIT_PASS;
881}
882
883/*-------------------------------------------------
884    sc3000_install_cartridge -
885-------------------------------------------------*/
886
887void sc3000_state::install_cartridge(UINT8 *ptr, int size)
888{
889   address_space *program = m_maincpu->memory().space(AS_PROGRAM);
890
891   /* include SG-1000 mapping */
892   sg1000_state::install_cartridge(ptr, size);
893
894   if (IS_CARTRIDGE_BASIC_LEVEL_III(ptr))
895   {
896      program->install_readwrite_bank(0x8000, 0xbfff, "bank1");
897      program->install_readwrite_bank(0xc000, 0xffff, "bank2");
898   }
899   else if (IS_CARTRIDGE_MUSIC_EDITOR(ptr))
900   {
901      program->install_readwrite_bank(0x8000, 0x9fff, "bank1");
902      program->install_readwrite_bank(0xc000, 0xc7ff, 0, 0x3800, "bank2");
903   }
904   else
905   {
906      /* regular cartridges */
907      program->install_readwrite_bank(0xc000, 0xc7ff, 0, 0x3800, "bank2");
908   }
909}
910
911/*-------------------------------------------------
912    DEVICE_IMAGE_LOAD( sc3000_cart )
913-------------------------------------------------*/
914
915static DEVICE_IMAGE_LOAD( sc3000_cart )
916{
917   running_machine &machine = image.device().machine();
918   sc3000_state *state = machine.driver_data<sc3000_state>();
919   UINT8 *ptr = machine.region(Z80_TAG)->base();
920   UINT32 size;
921
922   if (image.software_entry() == NULL)
923   {
924      size = image.length();
925      if (image.fread( ptr, size) != size)
926         return IMAGE_INIT_FAIL;
927   }
928   else
929   {
930      size = image.get_software_region_length("rom");
931      memcpy(ptr, image.get_software_region("rom"), size);
932   }
933
934   /* cartridge ROM and work RAM banking */
935   state->install_cartridge(ptr, size);
936
937   return IMAGE_INIT_PASS;
938}
939
940/***************************************************************************
941281    MACHINE INITIALIZATION
942282***************************************************************************/
943283
944284/*-------------------------------------------------
945    TIMER_CALLBACK( lightgun_tick )
946-------------------------------------------------*/
947
948static TIMER_CALLBACK( lightgun_tick )
949{
950   UINT8 *rom = machine.region(Z80_TAG)->base();
951
952   if (IS_CARTRIDGE_TV_DRAW(rom))
953   {
954      /* enable crosshair for TV Draw */
955      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_ALL);
956   }
957   else
958   {
959      /* disable crosshair for other cartridges */
960      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_NONE);
961   }
962}
963
964/*-------------------------------------------------
965    MACHINE_START( sg1000 )
966-------------------------------------------------*/
967
968void sg1000_state::machine_start()
969{
970   /* configure VDP */
971   TMS9928A_configure(&tms9928a_interface);
972
973   /* toggle light gun crosshair */
974   machine().scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
975
976   /* register for state saving */
977   state_save_register_global(machine(), m_tvdraw_data);
978}
979
980/*-------------------------------------------------
981    MACHINE_START( sc3000 )
982-------------------------------------------------*/
983
984void sc3000_state::machine_start()
985{
986   /* configure VDP */
987   TMS9928A_configure(&tms9928a_interface);
988
989   /* toggle light gun crosshair */
990   machine().scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
991
992   /* register for state saving */
993   state_save_register_global(machine(), m_tvdraw_data);
994   state_save_register_global(machine(), m_keylatch);
995}
996
997/*-------------------------------------------------
998285    sf7000_fdc_index_callback -
999286-------------------------------------------------*/
1000287
r11473r11474
1042329    MACHINE DRIVERS
1043330***************************************************************************/
1044331
1045/*-------------------------------------------------
1046    MACHINE_CONFIG_START( sg1000, sg1000_state )
1047-------------------------------------------------*/
1048332
1049static MACHINE_CONFIG_START( sg1000, sg1000_state )
1050   /* basic machine hardware */
1051   MCFG_CPU_ADD(Z80_TAG, Z80, XTAL_10_738635MHz/3)
1052   MCFG_CPU_PROGRAM_MAP(sg1000_map)
1053   MCFG_CPU_IO_MAP(sg1000_io_map)
1054   MCFG_CPU_VBLANK_INT(SCREEN_TAG, sg1000_int)
1055
1056    /* video hardware */
1057   MCFG_FRAGMENT_ADD(tms9928a)
1058   MCFG_SCREEN_MODIFY(SCREEN_TAG)
1059   MCFG_SCREEN_REFRESH_RATE((float)XTAL_10_738635MHz/2/342/262)
1060   MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(2500)) /* not accurate */
1061
1062   /* sound hardware */
1063   MCFG_SPEAKER_STANDARD_MONO("mono")
1064   MCFG_SOUND_ADD(SN76489A_TAG, SN76489A, XTAL_10_738635MHz/3)
1065   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
1066
1067   /* cartridge */
1068   MCFG_CARTSLOT_ADD("cart")
1069   MCFG_CARTSLOT_EXTENSION_LIST("sg,bin")
1070   MCFG_CARTSLOT_MANDATORY
1071   MCFG_CARTSLOT_INTERFACE("sg1000_cart")
1072   MCFG_CARTSLOT_LOAD(sg1000_cart)
1073
1074   /* software lists */
1075   MCFG_SOFTWARE_LIST_ADD("cart_list","sg1000")
1076
1077   /* internal ram */
1078   MCFG_RAM_ADD(RAM_TAG)
1079   MCFG_RAM_DEFAULT_SIZE("1K")
1080MACHINE_CONFIG_END
1081
1082333/*-------------------------------------------------
1083    MACHINE_CONFIG_DERIVED( omv, sg1000 )
1084-------------------------------------------------*/
1085
1086static MACHINE_CONFIG_DERIVED( omv, sg1000 )
1087   MCFG_CPU_MODIFY(Z80_TAG)
1088   MCFG_CPU_PROGRAM_MAP(omv_map)
1089   MCFG_CPU_IO_MAP(omv_io_map)
1090
1091   MCFG_CARTSLOT_MODIFY("cart")
1092   MCFG_CARTSLOT_EXTENSION_LIST("sg,bin")
1093   MCFG_CARTSLOT_NOT_MANDATORY
1094   MCFG_CARTSLOT_LOAD(omv_cart)
1095
1096   MCFG_RAM_MODIFY(RAM_TAG)
1097   MCFG_RAM_DEFAULT_SIZE("2K")
1098MACHINE_CONFIG_END
1099
1100/*-------------------------------------------------
1101    MACHINE_CONFIG_START( sc3000, sc3000_state )
1102-------------------------------------------------*/
1103
1104static MACHINE_CONFIG_START( sc3000, sc3000_state )
1105   /* basic machine hardware */
1106   MCFG_CPU_ADD(Z80_TAG, Z80, XTAL_10_738635MHz/3) // LH0080A
1107   MCFG_CPU_PROGRAM_MAP(sc3000_map)
1108   MCFG_CPU_IO_MAP(sc3000_io_map)
1109   MCFG_CPU_VBLANK_INT(SCREEN_TAG, sg1000_int)
1110
1111    /* video hardware */
1112   MCFG_FRAGMENT_ADD(tms9928a)
1113   MCFG_SCREEN_MODIFY(SCREEN_TAG)
1114   MCFG_SCREEN_REFRESH_RATE((float)XTAL_10_738635MHz/2/342/262)
1115   MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(2500)) /* not accurate */
1116
1117   /* sound hardware */
1118   MCFG_SPEAKER_STANDARD_MONO("mono")
1119   MCFG_SOUND_ADD(SN76489A_TAG, SN76489A, XTAL_10_738635MHz/3)
1120   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 1.00)
1121
1122   /* devices */
1123   MCFG_I8255_ADD(UPD9255_TAG, sc3000_ppi_intf)
1124//  MCFG_PRINTER_ADD("sp400") /* serial printer */
1125   MCFG_CASSETTE_ADD(CASSETTE_TAG, sc3000_cassette_config)
1126
1127   /* cartridge */
1128   MCFG_CARTSLOT_ADD("cart")
1129   MCFG_CARTSLOT_EXTENSION_LIST("sg,sc,bin")
1130   MCFG_CARTSLOT_INTERFACE("sg1000_cart")
1131   MCFG_CARTSLOT_LOAD(sc3000_cart)
1132
1133   /* software lists */
1134   MCFG_SOFTWARE_LIST_ADD("cart_list","sg1000")
1135
1136   /* internal ram */
1137   MCFG_RAM_ADD(RAM_TAG)
1138   MCFG_RAM_DEFAULT_SIZE("2K")
1139MACHINE_CONFIG_END
1140
1141/*-------------------------------------------------
1142334    MACHINE_CONFIG_START( sf7000, sf7000_state )
1143335-------------------------------------------------*/
1144336
r11473r11474
1179371    ROMS
1180372***************************************************************************/
1181373
1182ROM_START( sg1000 )
1183   ROM_REGION( 0x10000, Z80_TAG, ROMREGION_ERASE00 )
1184ROM_END
1185
1186#define rom_sg1000m2 rom_sg1000
1187
1188ROM_START( sc3000 )
1189   ROM_REGION( 0x10000, Z80_TAG, ROMREGION_ERASE00 )
1190   ROM_LOAD( "sc3000.rom", 0x0000, 0x8000, CRC(a46e3c73) SHA1(b1a8585a9afff962e8fd0e79b3305199da4e4562))
1191ROM_END
1192
1193#define rom_sc3000h rom_sg1000
1194
1195ROM_START( omv1000 )
1196   ROM_REGION( 0x10000, Z80_TAG, ROMREGION_ERASE00 )
1197   ROM_LOAD( "omvbios.bin", 0x0000, 0x4000, BAD_DUMP CRC(c5a67b95) SHA1(6d7c64dd60dee4a33061d3d3a7c2ed190d895cdb) )   // The BIOS comes from a Multivision FG-2000. It is still unknown if the FG-1000 BIOS differs
1198ROM_END
1199
1200ROM_START( omv2000 )
1201   ROM_REGION( 0x10000, Z80_TAG, ROMREGION_ERASE00 )
1202   ROM_LOAD( "omvbios.bin", 0x0000, 0x4000, CRC(c5a67b95) SHA1(6d7c64dd60dee4a33061d3d3a7c2ed190d895cdb) )
1203ROM_END
1204
1205374ROM_START( sf7000 )
1206375   ROM_REGION( 0x10000, Z80_TAG, 0 )
1207376   ROM_LOAD( "ipl.rom", 0x0000, 0x2000, CRC(d76810b8) SHA1(77339a6db2593aadc638bed77b8e9bed5d9d87e3) )
r11473r11474
1212381***************************************************************************/
1213382
1214383/*    YEAR  NAME        PARENT      COMPAT      MACHINE     INPUT       INIT    COMPANY             FULLNAME                                    FLAGS */
1215CONS( 1983,   sg1000,     0,          0,          sg1000,     sg1000,     0,      "Sega",             "SG-1000",                                  GAME_SUPPORTS_SAVE )
1216CONS( 1984,   sg1000m2,   sg1000,     0,          sc3000,     sc3000,     0,      "Sega",             "SG-1000 II",                               GAME_SUPPORTS_SAVE )
1217COMP( 1983,   sc3000,     0,          sg1000,     sc3000,     sc3000,     0,      "Sega",             "SC-3000",                                  GAME_SUPPORTS_SAVE )
1218COMP( 1983,   sc3000h,    sc3000,     0,          sc3000,     sc3000,     0,      "Sega",             "SC-3000H",                                 GAME_SUPPORTS_SAVE )
1219384COMP( 1983,   sf7000,     sc3000,     0,          sf7000,     sf7000,     0,      "Sega",             "SC-3000/Super Control Station SF-7000",    GAME_SUPPORTS_SAVE )
1220CONS( 1984,   omv1000,    sg1000,     0,          omv,        omv,        0,      "Tsukuda Original", "Othello Multivision FG-1000",              GAME_SUPPORTS_SAVE )
1221CONS( 1984,   omv2000,    sg1000,     0,          omv,        omv,        0,      "Tsukuda Original", "Othello Multivision FG-2000",              GAME_SUPPORTS_SAVE )
src/mess/mess.mak
r11473r11474
277277   $(MESSOBJ)/camputers.a \
278278   $(MESSOBJ)/canon.a \
279279   $(MESSOBJ)/cantab.a \
280   $(MESSOBJ)/capcom.a \
281280   $(MESSOBJ)/casio.a \
282281   $(MESSOBJ)/cbm.a \
283282   $(MESSOBJ)/cccp.a \
r11473r11474
441440   $(MAME_VIDEO)/atari.o      \
442441   $(MAME_VIDEO)/antic.o      \
443442   $(MAME_VIDEO)/gtia.o      \
443   $(MAME_DRIVERS)/jaguar.o   \
444444   $(MAME_AUDIO)/jaguar.o      \
445445   $(MAME_VIDEO)/jaguar.o      \
446446   $(MAME_VIDEO)/astrocde.o   \
r11473r11474
530530   $(MAME_MACHINE)/psxcd.o      \
531531   $(MAME_MACHINE)/psxcddrv.o   \
532532   $(MAME_MACHINE)/psxcard.o   \
533   $(MAME_DRIVERS)/saturn.o   \
534   $(MAME_DRIVERS)/cps1.o   \
535   $(MAME_VIDEO)/cps1.o   \
533536
534537#-------------------------------------------------
535538# the following files are general components and
r11473r11474
561564   $(MESS_MACHINE)/mm58274c.o   \
562565   $(MESS_MACHINE)/mpc105.o   \
563566   $(MESS_MACHINE)/mos6530.o   \
567   $(MESS_MACHINE)/msm8251.o   \
568   $(MESS_MACHINE)/serial.o   \
569   $(MESS_MACHINE)/upd765.o   \
564570   $(MESS_MACHINE)/ncr5380.o   \
565571   $(MESS_MACHINE)/pc_lpt.o   \
566572   $(MESS_MACHINE)/pc_mouse.o   \
r11473r11474
675681   $(MESS_DRIVERS)/a7800.o      \
676682   $(MESS_VIDEO)/a7800.o      \
677683   $(MESS_DRIVERS)/a2600.o      \
678   $(MAME_DRIVERS)/jaguar.o   \
679684   $(MESS_DRIVERS)/atarist.o   \
680685   $(MESS_VIDEO)/atarist.o    \
681686   $(MESS_DRIVERS)/lynx.o      \
r11473r11474
722727   $(MESS_DRIVERS)/ace.o      \
723728   $(MESS_FORMATS)/ace_ace.o   \
724729
725$(MESSOBJ)/capcom.a:         \
726   $(MAME_DRIVERS)/cps1.o   \
727   $(MAME_VIDEO)/cps1.o   \
728
729730$(MESSOBJ)/casio.a:            \
730731   $(MESS_DRIVERS)/casloopy.o   \
731732   $(MESS_DRIVERS)/cfx9850.o   \
r11473r11474
13361337   $(MESS_VIDEO)/mbc55x.o     \
13371338
13381339$(MESSOBJ)/sega.a:            \
1339   $(MAME_DRIVERS)/saturn.o   \
1340   $(MESS_DRIVERS)/sg1000.o    \
13401341
13411342$(MESSOBJ)/sgi.a:            \
13421343   $(MESS_MACHINE)/sgi.o      \

Previous 15994 Revisions Next


© 1998-2018 The MESS Team