mirror of
https://github.com/dkgrizzly/GreenSCSI.git
synced 2025-01-24 23:33:40 +00:00
Configuration CLI, performance tweaks, stability improvements, tested on GreenSCSI v1.0 Minimum Viable Product (MVP) PCB
This commit is contained in:
parent
c2bac1a9d0
commit
494a803890
1440
src/GreenSCSI.ino
1440
src/GreenSCSI.ino
File diff suppressed because it is too large
Load Diff
37
src/cmd.h
Executable file
37
src/cmd.h
Executable file
@ -0,0 +1,37 @@
|
||||
#ifndef __CMD_H
|
||||
#define __CMD_H
|
||||
|
||||
#define MAX_MSG_SIZE 256
|
||||
#define MAXARG 32
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// command line structure
|
||||
typedef struct Commands_s {
|
||||
const char *Name;
|
||||
const char *ValidPath;
|
||||
int MinParams;
|
||||
const char *ShortHelp;
|
||||
const char *FullHelp;
|
||||
void (*Func)(int argc, char **argv);
|
||||
struct Commands_s *Dispatch;
|
||||
} Commands_t;
|
||||
|
||||
void cmdDisplay();
|
||||
void cmdParse(char *cmd);
|
||||
void cmdPoll();
|
||||
|
||||
void cmdCommandHelp(boolean singleCommand, Commands_t *table, int cmd);
|
||||
void cmdDispatchHelp(Commands_t *table, int argc, char **argv);
|
||||
void cmdDispatch(Commands_t *table, int argc, char **argv);
|
||||
|
||||
int execHandler(char *filename);
|
||||
int execLoop();
|
||||
|
||||
void execcmd(int argc, char **argv);
|
||||
void showcmd(int argc, char **argv);
|
||||
void setcmd(int argc, char **argv);
|
||||
|
||||
extern Commands_t GlobalCommands[];
|
||||
|
||||
#endif /* __CMD_H */
|
1379
src/cmd.ino
Executable file
1379
src/cmd.ino
Executable file
File diff suppressed because it is too large
Load Diff
35
src/config.h
Executable file
35
src/config.h
Executable file
@ -0,0 +1,35 @@
|
||||
#ifndef __CONFIG_H
|
||||
#define __CONFIG_H
|
||||
|
||||
#define DEBUG 0 // 0:No debug information output (Faster by 2-3x+)
|
||||
// 1: Debug information output to USB Serial
|
||||
// 2: Debug information output to LOG.txt (slow)
|
||||
|
||||
#define SYNCHRO false // Support Synchronous mode.
|
||||
#define READ_SPEED_OPTIMIZE true //
|
||||
#define WRITE_SPEED_OPTIMIZE true //
|
||||
#define USE_DB2ID_TABLE true // Use table to get ID from SEL-DB
|
||||
|
||||
// SCSI config
|
||||
#define NUM_SCSIID 8 // Maximum number of supported SCSI-IDs (The minimum is 1)
|
||||
#define NUM_SCSILUN 8 // Maximum number of LUNs supported (The minimum is 1)
|
||||
#define NUM_VDEV 8 // Maximum number of VDEVs supported (The minimum is 1)
|
||||
|
||||
#define READ_PARITY_CHECK 0 // Perform read parity check (unverified)
|
||||
|
||||
// HDD format
|
||||
#define MAX_BLOCKSIZE (1 << 15) // Maximum BLOCK size (2048 to 8192 tested, 16384 had issues)
|
||||
|
||||
|
||||
// Supported Device Types
|
||||
#define SUPPORT_DISK true
|
||||
#define SUPPORT_OPTICAL true
|
||||
#define SUPPORT_TAPE false
|
||||
|
||||
|
||||
// Compatibility Settings
|
||||
#define SUPPORT_SASI false // Enable SASI compatiblity for Sharp X68000
|
||||
#define SUPPORT_SASI_DEFAULT false // Turn it on by default
|
||||
|
||||
|
||||
#endif /* __CONFIG_H */
|
75
src/defaults.ino
Executable file
75
src/defaults.ino
Executable file
@ -0,0 +1,75 @@
|
||||
#include "config.h"
|
||||
|
||||
// SCSI Drive Vendor information
|
||||
#if SUPPORT_DISK
|
||||
static uint8_t SCSI_DISK_INQUIRY_RESPONSE[96] = {
|
||||
0x00, //device type
|
||||
0x00, //RMB = 0
|
||||
0x05, //ISO, ECMA, ANSI version
|
||||
0x02, //Response data format
|
||||
35 - 4, //Additional data length
|
||||
0, 0, //Reserve
|
||||
0x00, //Support function
|
||||
'Q', 'U', 'A', 'N', 'T', 'U', 'M', ' ', // vendor 8
|
||||
'F', 'I', 'R', 'E', 'B', 'A', 'L', 'L', '1', ' ', ' ',' ', ' ', ' ', ' ', ' ', // product 16
|
||||
'1', '.', '0', ' ', // version 4
|
||||
// Release Number (1 Byte)
|
||||
0x20,
|
||||
// Revision Date (10 Bytes)
|
||||
'2','0','2','1','/','1','1','/','2','2',
|
||||
0
|
||||
};
|
||||
#endif /* SUPPORT_DISK */
|
||||
|
||||
#if SUPPORT_SASI
|
||||
static uint8_t SCSI_NEC_INQUIRY_RESPONSE[96] = {
|
||||
0x00, //Device type
|
||||
0x00, //RMB = 0
|
||||
0x01, //ISO,ECMA,ANSI version
|
||||
0x01, //Response data format
|
||||
35 - 4, //Additional data length
|
||||
0, 0, //Reserve
|
||||
0x00, //Support function
|
||||
'N', 'E', 'C', 'I', 'T', 'S', 'U', ' ',
|
||||
'A', 'r', 'd', 'S', 'C', 'S', 'i', 'n', 'o', ' ', ' ',' ', ' ', ' ', ' ', ' ',
|
||||
'0', '0', '1', '0',
|
||||
0
|
||||
};
|
||||
#endif /* SUPPORT_SASI */
|
||||
|
||||
#if SUPPORT_OPTICAL
|
||||
static uint8_t SCSI_CDROM_INQUIRY_RESPONSE[96] = {
|
||||
0x05, //device type
|
||||
0x80, //RMB = 0
|
||||
0x05, //ISO, ECMA, ANSI version
|
||||
0x02, //Response data format
|
||||
46 - 4, //Additional data length
|
||||
0, 0, //Reserve
|
||||
0x00, //Support function
|
||||
'S', 'O', 'N', 'Y', ' ', ' ', ' ', ' ', // vendor 8
|
||||
'C', 'D', '-', 'R', 'O', 'M', ' ', 'C', 'D', 'U', '-','8', '0', '0', '3', ' ', // product 16
|
||||
'1', '.', '7', 'w', // version 4
|
||||
// Release Number (1 Byte)
|
||||
0x20,
|
||||
// Revision Date (10 Bytes)
|
||||
'2','0','2','1','/','1','1','/','2','2',
|
||||
0
|
||||
};
|
||||
#endif /* SUPPORT_OPTICAL */
|
||||
|
||||
#if SUPPORT_TAPE
|
||||
static uint8_t SCSI_TAPE_INQUIRY_RESPONSE[96] = {
|
||||
0x01, //device type
|
||||
0x80, //RMB = 0
|
||||
0x03, //ISO, ECMA, ANSI version
|
||||
0x02, //Response data format
|
||||
37 - 4, //Additional data length
|
||||
0, //Reserve
|
||||
0x00, // Support function
|
||||
0x00, // Support function
|
||||
'I', 'B', 'M', ' ', ' ', ' ', ' ', ' ', // vendor 8
|
||||
'U', 'L', 'T', '3', '5', '8', '0', '-', 'T', 'D', '1',' ', ' ', ' ', ' ', ' ', // product 16
|
||||
'M', 'B', 'N', '0', // version 4
|
||||
0
|
||||
};
|
||||
#endif /* SUPPORT_TAPE */
|
103
src/disk.ino
103
src/disk.ino
@ -1,3 +1,4 @@
|
||||
#include "config.h"
|
||||
#include "scsi_defs.h"
|
||||
|
||||
void Read6CommandHandler() {
|
||||
@ -22,14 +23,14 @@ void Seek6CommandHandler() {
|
||||
*/
|
||||
void ReadCapacityCommandHandler() {
|
||||
LOGN("[ReadCapacity]");
|
||||
if(!m_img) {
|
||||
if(!m_sel) {
|
||||
m_sts |= 0x02; // Image file absent
|
||||
m_phase = PHASE_STATUSIN;
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t bl = m_img->m_blocksize;
|
||||
uint32_t bc = m_img->m_fileSize / bl;
|
||||
uint32_t bl = m_sel->m_blocksize;
|
||||
uint32_t bc = m_sel->m_fileSize / bl;
|
||||
uint8_t buf[8] = {
|
||||
(uint8_t)(((uint32_t)(bc >> 24))&0xff), (uint8_t)(((uint32_t)(bc >> 16))&0xff), (uint8_t)(((uint32_t)(bc >> 8))&0xff), (uint8_t)(((uint32_t)(bc))&0xff),
|
||||
(uint8_t)(((uint32_t)(bl >> 24))&0xff), (uint8_t)(((uint32_t)(bl >> 16))&0xff), (uint8_t)(((uint32_t)(bl >> 8))&0xff), (uint8_t)(((uint32_t)(bl))&0xff)
|
||||
@ -65,7 +66,7 @@ uint8_t onReadCommand(uint32_t adds, uint32_t len)
|
||||
LOG(" ");
|
||||
LOGHEX4N(len);
|
||||
|
||||
if(!m_img) return 0x02; // Image file absent
|
||||
if(!m_sel) return 0x02; // Image file absent
|
||||
|
||||
LED_ON();
|
||||
writeDataPhaseSD(adds, len);
|
||||
@ -83,7 +84,7 @@ uint8_t onWriteCommand(uint32_t adds, uint32_t len)
|
||||
LOG(" ");
|
||||
LOGHEX4N(len);
|
||||
|
||||
if(!m_img) return 0x02; // Image file absent
|
||||
if(!m_sel) return 0x02; // Image file absent
|
||||
|
||||
LED_ON();
|
||||
readDataPhaseSD(adds, len);
|
||||
@ -91,30 +92,76 @@ uint8_t onWriteCommand(uint32_t adds, uint32_t len)
|
||||
return 0; //sts
|
||||
}
|
||||
|
||||
void ConfigureDiskHandlers(int id) {
|
||||
void ConfigureDiskHandlers(VirtualDevice_t *vdev) {
|
||||
for(int c = 0; c < 256; c++)
|
||||
m_handler[id][c] = &UnknownCommandHandler;
|
||||
vdev->m_handler[c] = &UnknownCommandHandler;
|
||||
|
||||
m_handler[id][CMD_TEST_UNIT_READY] = &TestUnitCommandHandler;
|
||||
m_handler[id][CMD_REZERO_UNIT] = &RezeroUnitCommandHandler;
|
||||
m_handler[id][CMD_REQUEST_SENSE] = &RequestSenseCommandHandler;
|
||||
m_handler[id][CMD_FORMAT_UNIT] = &FormatUnitCommandHandler;
|
||||
m_handler[id][0x06] = &FormatUnitCommandHandler;
|
||||
m_handler[id][0x07] = &ReassignBlocksCommandHandler;
|
||||
m_handler[id][CMD_READ6] = &Read6CommandHandler;
|
||||
m_handler[id][CMD_WRITE6] = &Write6CommandHandler;
|
||||
m_handler[id][CMD_SEEK6] = &Seek6CommandHandler;
|
||||
m_handler[id][CMD_INQUIRY] = &InquiryCommandHandler;
|
||||
m_handler[id][CMD_MODE_SELECT6] = &ModeSelect6CommandHandler;
|
||||
m_handler[id][CMD_MODE_SENSE6] = &ModeSense6CommandHandler;
|
||||
m_handler[id][CMD_START_STOP_UNIT] = &StartStopUnitCommandHandler;
|
||||
m_handler[id][CMD_PREVENT_REMOVAL] = &PreAllowMediumRemovalCommandHandler;
|
||||
m_handler[id][CMD_READ_CAPACITY10] = &ReadCapacityCommandHandler;
|
||||
m_handler[id][CMD_READ10] = &Read10CommandHandler;
|
||||
m_handler[id][CMD_WRITE10] = &Write10CommandHandler;
|
||||
m_handler[id][CMD_SEEK10] = &Seek10CommandHandler;
|
||||
m_handler[id][CMD_MODE_SENSE10] = &ModeSense10CommandHandler;
|
||||
#if SCSI_SELECT == 1
|
||||
m_handler[id][0xC2] = &DTCsetDriveParameterCommandHandler;
|
||||
vdev->m_handler[CMD_TEST_UNIT_READY] = &TestUnitCommandHandler;
|
||||
vdev->m_handler[CMD_REZERO_UNIT] = &RezeroUnitCommandHandler;
|
||||
vdev->m_handler[CMD_REQUEST_SENSE] = &RequestSenseCommandHandler;
|
||||
vdev->m_handler[CMD_FORMAT_UNIT] = &FormatUnitCommandHandler;
|
||||
vdev->m_handler[CMD_FORMAT_UNIT_ALT] = &FormatUnitCommandHandler;
|
||||
vdev->m_handler[CMD_REASSIGN_BLOCKS] = &ReassignBlocksCommandHandler;
|
||||
vdev->m_handler[CMD_READ6] = &Read6CommandHandler;
|
||||
vdev->m_handler[CMD_WRITE6] = &Write6CommandHandler;
|
||||
vdev->m_handler[CMD_SEEK6] = &Seek6CommandHandler;
|
||||
vdev->m_handler[CMD_INQUIRY] = &InquiryCommandHandler;
|
||||
vdev->m_handler[CMD_MODE_SELECT6] = &ModeSelect6CommandHandler;
|
||||
vdev->m_handler[CMD_MODE_SENSE6] = &ModeSenseCommandHandler;
|
||||
vdev->m_handler[CMD_START_STOP_UNIT] = &StartStopUnitCommandHandler;
|
||||
vdev->m_handler[CMD_PREVENT_REMOVAL] = &PreAllowMediumRemovalCommandHandler;
|
||||
vdev->m_handler[CMD_READ_CAPACITY10] = &ReadCapacityCommandHandler;
|
||||
vdev->m_handler[CMD_READ10] = &Read10CommandHandler;
|
||||
vdev->m_handler[CMD_WRITE10] = &Write10CommandHandler;
|
||||
vdev->m_handler[CMD_SEEK10] = &Seek10CommandHandler;
|
||||
vdev->m_handler[CMD_MODE_SENSE10] = &ModeSenseCommandHandler;
|
||||
#if SUPPORT_SASI
|
||||
if(m_sasi_mode)
|
||||
vdev->m_handler[CMD_SET_DRIVE_PARAMETER] = &DTCsetDriveParameterCommandHandler;
|
||||
#endif
|
||||
}
|
||||
|
||||
// If config file exists, read the first three lines and copy the contents.
|
||||
// File must be well formed or you will get junk in the SCSI Vendor fields.
|
||||
void ConfigureDisk(VirtualDevice_t *vdev, const char *image_name) {
|
||||
memcpy(vdev->m_inquiryresponse, SCSI_DISK_INQUIRY_RESPONSE, sizeof(SCSI_DISK_INQUIRY_RESPONSE));
|
||||
|
||||
if(image_name) {
|
||||
char configname[MAX_FILE_PATH+1];
|
||||
memcpy(configname, image_name, MAX_FILE_PATH+1);
|
||||
char *psuffix = strstr(configname, ".img");
|
||||
if(psuffix) {
|
||||
strcpy(psuffix, ".cfg");
|
||||
} else {
|
||||
sprintf(configname, "hd%d%d.cfg", vdev->m_id, vdev->m_lun);
|
||||
}
|
||||
|
||||
FsFile config_file = sd.open(configname, O_RDONLY);
|
||||
if (config_file.isOpen()) {
|
||||
char vendor[9];
|
||||
memset(vendor, 0, sizeof(vendor));
|
||||
config_file.readBytes(vendor, sizeof(vendor));
|
||||
LOGN("SCSI VENDOR: ");
|
||||
LOGN(vendor);
|
||||
memcpy(&(vdev->m_inquiryresponse[8]), vendor, 8);
|
||||
|
||||
char product[17];
|
||||
memset(product, 0, sizeof(product));
|
||||
config_file.readBytes(product, sizeof(product));
|
||||
LOGN("SCSI PRODUCT: ");
|
||||
LOGN(product);
|
||||
memcpy(&(vdev->m_inquiryresponse[16]), product, 16);
|
||||
|
||||
char version[5];
|
||||
memset(version, 0, sizeof(version));
|
||||
config_file.readBytes(version, sizeof(version));
|
||||
LOGN("SCSI VERSION: ");
|
||||
LOGN(version);
|
||||
memcpy(&(vdev->m_inquiryresponse[32]), version, 4);
|
||||
config_file.close();
|
||||
}
|
||||
}
|
||||
|
||||
vdev->m_type = DEV_DISK;
|
||||
ConfigureDiskHandlers(vdev);
|
||||
}
|
||||
|
327
src/general.ino
327
src/general.ino
@ -1,3 +1,4 @@
|
||||
#include "config.h"
|
||||
#include "scsi_defs.h"
|
||||
|
||||
|
||||
@ -6,13 +7,20 @@
|
||||
*/
|
||||
void InquiryCommandHandler() {
|
||||
LOGN("[Inquiry]");
|
||||
uint8_t len = m_cmd[4];
|
||||
uint8_t buf[36];
|
||||
memcpy(buf, m_inquiryresponse[m_id], 36);
|
||||
uint8_t len;
|
||||
|
||||
if(!m_img) buf[0] = 0x7f;
|
||||
if(!m_sel) {
|
||||
memset(m_responsebuffer, 0, 96);
|
||||
m_responsebuffer[0] = 0x7f;
|
||||
m_responsebuffer[4] = 35 - 4;
|
||||
len = 36;
|
||||
} else {
|
||||
memcpy(m_responsebuffer, m_sel->m_inquiryresponse, 96);
|
||||
len = m_responsebuffer[4] + 5;
|
||||
}
|
||||
|
||||
writeDataPhase(len < 36 ? len : 36, buf);
|
||||
len = min(len, m_cmd[4]);
|
||||
writeDataPhase(len, m_responsebuffer);
|
||||
m_phase = PHASE_STATUSIN;
|
||||
}
|
||||
|
||||
@ -31,13 +39,13 @@ void RequestSenseCommandHandler() {
|
||||
0,
|
||||
};
|
||||
|
||||
if(!m_img) {
|
||||
if(!m_sel) {
|
||||
// Image file absent
|
||||
buf[2] = 0x02; // NOT_READY
|
||||
buf[12] = 0x25; // Logical Unit Not Supported
|
||||
} else {
|
||||
buf[2] = m_sense[m_id][m_lun].m_key;
|
||||
m_sense[m_id][m_lun].m_key = 0;
|
||||
buf[2] = m_sel->m_sense.m_key;
|
||||
m_sel->m_sense.m_key = 0;
|
||||
}
|
||||
writeDataPhase(len < 18 ? len : 18, buf);
|
||||
m_phase = PHASE_STATUSIN;
|
||||
@ -45,10 +53,10 @@ void RequestSenseCommandHandler() {
|
||||
|
||||
void TestUnitCommandHandler() {
|
||||
LOGN("[TestUnit]");
|
||||
if(!m_img) {
|
||||
if(!m_sel) {
|
||||
// Image file absent
|
||||
m_sense[m_id][m_lun].m_key = NOT_READY; // NOT_READY
|
||||
m_sense[m_id][m_lun].m_code = NO_MEDIA; // Logical Unit Not Supported
|
||||
m_sel->m_sense.m_key = NOT_READY; // NOT_READY
|
||||
m_sel->m_sense.m_code = NO_MEDIA; // Logical Unit Not Supported
|
||||
}
|
||||
m_phase = PHASE_STATUSIN;
|
||||
}
|
||||
@ -68,28 +76,295 @@ void ReassignBlocksCommandHandler() {
|
||||
m_phase = PHASE_STATUSIN;
|
||||
}
|
||||
|
||||
void ModeSense6CommandHandler() {
|
||||
LOGN("[ModeSense6]");
|
||||
m_sts |= onModeSenseCommand(m_cmd[1]&0x80, m_cmd[2], m_cmd[4]);
|
||||
/*
|
||||
* MODE SENSE command processing.
|
||||
*/
|
||||
void ModeSenseCommandHandler()
|
||||
{
|
||||
uint16_t len, maxlen;
|
||||
int page, pagemax, pagemin;
|
||||
|
||||
switch(m_cmd[0]) {
|
||||
case CMD_MODE_SENSE6:
|
||||
LOGN("[ModeSense6]");
|
||||
maxlen = m_cmd[4];
|
||||
len = 1;
|
||||
break;
|
||||
case CMD_MODE_SENSE10:
|
||||
LOGN("[ModeSense10]");
|
||||
maxlen = ((uint16_t)m_cmd[7] << 8) | m_cmd[8];
|
||||
len = 2;
|
||||
break;
|
||||
default:
|
||||
m_sts |= 0x02;
|
||||
m_phase = PHASE_STATUSIN;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Check whether medium is present */
|
||||
if(!m_sel) {
|
||||
m_sts |= 0x02;
|
||||
m_phase = PHASE_STATUSIN;
|
||||
return;
|
||||
}
|
||||
|
||||
memset(m_responsebuffer, 0, sizeof(m_responsebuffer));
|
||||
#if 0
|
||||
if(m_sasi_mode) {
|
||||
int pageCode = cmd2 & 0x3F;
|
||||
|
||||
// Assuming sector size 512, number of sectors 25, number of heads 8 as default settings
|
||||
int size = m_sel->m_fileSize;
|
||||
int cylinders = (int)(size >> 9);
|
||||
cylinders >>= 3;
|
||||
cylinders /= 25;
|
||||
int sectorsize = 512;
|
||||
int sectors = 25;
|
||||
int heads = 8;
|
||||
// Sector size
|
||||
int disksize = 0;
|
||||
for(disksize = 16; disksize > 0; --(disksize)) {
|
||||
if ((1 << disksize) == sectorsize)
|
||||
break;
|
||||
}
|
||||
// Number of blocks
|
||||
uint32_t diskblocks = (uint32_t)(size >> disksize);
|
||||
int a = 4;
|
||||
if(dbd == 0) {
|
||||
uint32_t bl = m_sel->m_blocksize;
|
||||
uint32_t bc = m_sel->m_fileSize / bl;
|
||||
uint8_t c[8] = {
|
||||
0,// Density code
|
||||
bc >> 16, bc >> 8, bc,
|
||||
0, //Reserve
|
||||
bl >> 16, bl >> 8, bl
|
||||
};
|
||||
memcpy(&m_responsebuffer[4], c, 8);
|
||||
a += 8;
|
||||
m_responsebuffer[3] = 0x08;
|
||||
}
|
||||
switch(pageCode) {
|
||||
case 0x3F:
|
||||
{
|
||||
m_responsebuffer[len + 0] = 0x01;
|
||||
m_responsebuffer[len + 1] = 0x06;
|
||||
a += 8;
|
||||
}
|
||||
case 0x03: // drive parameters
|
||||
{
|
||||
m_responsebuffer[len + 0] = 0x80 | 0x03; // Page code
|
||||
m_responsebuffer[len + 1] = 0x16; // Page length
|
||||
m_responsebuffer[len + 2] = (uint8_t)(heads >> 8);// number of sectors / track
|
||||
m_responsebuffer[len + 3] = (uint8_t)(heads);// number of sectors / track
|
||||
m_responsebuffer[len + 10] = (uint8_t)(sectors >> 8);// number of sectors / track
|
||||
m_responsebuffer[len + 11] = (uint8_t)(sectors);// number of sectors / track
|
||||
int size = 1 << disksize;
|
||||
m_responsebuffer[len + 12] = (uint8_t)(size >> 8);// number of sectors / track
|
||||
m_responsebuffer[len + 13] = (uint8_t)(size);// number of sectors / track
|
||||
a += 24;
|
||||
if(pageCode != 0x3F) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
case 0x04: // drive parameters
|
||||
{
|
||||
LOGN("AddDrive");
|
||||
m_responsebuffer[len + 0] = 0x04; // Page code
|
||||
m_responsebuffer[len + 1] = 0x12; // Page length
|
||||
m_responsebuffer[len + 2] = (cylinders >> 16);// Cylinder length
|
||||
m_responsebuffer[len + 3] = (cylinders >> 8);
|
||||
m_responsebuffer[len + 4] = cylinders;
|
||||
m_responsebuffer[len + 5] = heads; // Number of heads
|
||||
a += 20;
|
||||
if(pageCode != 0x3F) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
m_responsebuffer[0] = a - 1;
|
||||
writeDataPhase(len < a ? len : a, m_responsebuffer);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
/* Default medium type */
|
||||
m_responsebuffer[len++] = (m_sel->m_type == DEV_OPTICAL) ? 0xf0 : 0x00;
|
||||
/* Write protected */
|
||||
m_responsebuffer[len++] = (m_sel->m_type == DEV_OPTICAL) ? 0x80 : 0x00;
|
||||
|
||||
// ModeSense10 has two extra bytes, and Block Descriptor Length has an extra MSB
|
||||
if(m_cmd[0] == CMD_MODE_SENSE10) {
|
||||
len += 2;
|
||||
m_responsebuffer[len++] = 0;
|
||||
}
|
||||
|
||||
/* Add block descriptor if DBD is not set */
|
||||
if (m_cmd[1] & 0x08) {
|
||||
m_responsebuffer[len++] = 0; /* No block descriptor */
|
||||
} else {
|
||||
if(m_sel->m_type == DEV_TAPE) {
|
||||
m_responsebuffer[len++] = 8; /* Block descriptor length */
|
||||
m_responsebuffer[len++] = 0x40; /* Medium Density Code */
|
||||
m_responsebuffer[len++] = 0x00; /* Number of Blocks (0) */
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
m_responsebuffer[len++] = 0x00; /* Block Length 1024 */
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
m_responsebuffer[len++] = 0x04;
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
} else {
|
||||
uint32_t capacity = (m_sel->m_fileSize / m_sel->m_blocksize);
|
||||
m_responsebuffer[len++] = 8; /* Block descriptor length */
|
||||
m_responsebuffer[len++] = (capacity >> 24) & 0xff;
|
||||
m_responsebuffer[len++] = (capacity >> 16) & 0xff;
|
||||
m_responsebuffer[len++] = (capacity >> 8) & 0xff;
|
||||
m_responsebuffer[len++] = capacity & 0xff;
|
||||
m_responsebuffer[len++] = (m_sel->m_blocksize >> 24) & 0xff;
|
||||
m_responsebuffer[len++] = (m_sel->m_blocksize >> 16) & 0xff;
|
||||
m_responsebuffer[len++] = (m_sel->m_blocksize >> 8) & 0xff;
|
||||
m_responsebuffer[len++] = (m_sel->m_blocksize) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check for requested mode page */
|
||||
page = m_cmd[2] & 0x3F;
|
||||
pagemax = (page != 0x3f) ? page : 0x3e;
|
||||
pagemin = (page != 0x3f) ? page : 0x00;
|
||||
for(page = pagemax; page >= pagemin; page--) {
|
||||
switch (page) {
|
||||
case MODEPAGE_VENDOR_SPECIFIC:
|
||||
/* Accept request only for current values */
|
||||
if (m_cmd[2] & 0xC0) {
|
||||
//DEBUGPRINT(DBG_TRACE, " [2]==%d", m_cmd[2]);
|
||||
/* Prepare sense data */
|
||||
m_sts |= STATUS_CHECK;
|
||||
m_sel->m_sense.m_key = ILLEGAL_REQUEST;
|
||||
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
|
||||
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
|
||||
m_sel->m_sense.m_key_specific[1] = 0x00;
|
||||
m_sel->m_sense.m_key_specific[2] = 0x02;
|
||||
m_phase = PHASE_STATUSIN;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Unit attention */
|
||||
m_responsebuffer[len++] = 0x80; // PS, page id
|
||||
m_responsebuffer[len++] = 0x02; // Page length
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
break;
|
||||
case MODEPAGE_RW_ERROR_RECOVERY:
|
||||
if(m_cmd[2] & 0x40) {
|
||||
m_sts |= STATUS_CHECK;
|
||||
m_sel->m_sense.m_key = ILLEGAL_REQUEST;
|
||||
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
|
||||
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
|
||||
m_sel->m_sense.m_key_specific[1] = 0x00;
|
||||
m_sel->m_sense.m_key_specific[2] = 0x02;
|
||||
m_phase = PHASE_STATUSIN;
|
||||
return;
|
||||
}
|
||||
m_responsebuffer[len++] = MODEPAGE_RW_ERROR_RECOVERY; // PS, page id
|
||||
m_responsebuffer[len++] = 0x0a; // Page length
|
||||
m_responsebuffer[len++] = 0x05; //
|
||||
m_responsebuffer[len++] = 0x00; // Read Retry Count
|
||||
m_responsebuffer[len++] = 0x00; // Reserved
|
||||
m_responsebuffer[len++] = 0x00; // Reserved
|
||||
m_responsebuffer[len++] = 0x00; // Reserved
|
||||
m_responsebuffer[len++] = 0x00; // Reserved
|
||||
m_responsebuffer[len++] = 0x00; // Write Retry Count
|
||||
m_responsebuffer[len++] = 0x00; // Reserved
|
||||
m_responsebuffer[len++] = 0x00; // Recovery Time Limit
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
break;
|
||||
#if 0
|
||||
case MODEPAGE_DCRC_PARAMETERS:
|
||||
m_responsebuffer[len++] = 0x82; // PS, page id
|
||||
m_responsebuffer[len++] = 0x0e; // Page length
|
||||
m_responsebuffer[len++] = 0xe6; // Buffer full ratio, 90%
|
||||
m_responsebuffer[len++] = 0x1a; // Buffer empty ratio, 10%
|
||||
m_responsebuffer[len++] = 0x00; // Bus inactivity limit
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
m_responsebuffer[len++] = 0x00; // Disconnect time limit
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
m_responsebuffer[len++] = 0x00; // Connect time limit
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
m_responsebuffer[len++] = 0x00; // Maximum burst size
|
||||
m_responsebuffer[len++] = 0x00;
|
||||
m_responsebuffer[len++] = 0x00; // EMDP, Dimm, DTDC
|
||||
m_responsebuffer[len++] = 0x00; // Reserved
|
||||
m_responsebuffer[len++] = 0x00; // Reserved
|
||||
m_responsebuffer[len++] = 0x00; // Reserved
|
||||
break;
|
||||
#endif
|
||||
case MODEPAGE_FORMAT_PARAMETERS:
|
||||
m_responsebuffer[len + 0] = MODEPAGE_FORMAT_PARAMETERS; //Page code
|
||||
m_responsebuffer[len + 1] = 0x16; // Page length
|
||||
m_responsebuffer[len + 11] = 0x3F;//Number of sectors / track
|
||||
len += 24;
|
||||
break;
|
||||
case MODEPAGE_RIGID_GEOMETRY:
|
||||
{
|
||||
uint32_t bc = m_sel->m_fileSize / m_sel->m_file;
|
||||
m_responsebuffer[len + 0] = MODEPAGE_RIGID_GEOMETRY; //Page code
|
||||
m_responsebuffer[len + 1] = 0x16; // Page length
|
||||
m_responsebuffer[len + 2] = bc >> 16;// Cylinder length
|
||||
m_responsebuffer[len + 3] = bc >> 8;
|
||||
m_responsebuffer[len + 4] = bc;
|
||||
m_responsebuffer[len + 5] = 1; //Number of heads
|
||||
len += 24;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if(pagemin == pagemax) {
|
||||
m_sts |= STATUS_CHECK;
|
||||
m_sel->m_sense.m_key = ILLEGAL_REQUEST;
|
||||
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
|
||||
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
|
||||
m_sel->m_sense.m_key_specific[1] = 0x00;
|
||||
m_sel->m_sense.m_key_specific[2] = 0x02;
|
||||
m_phase = PHASE_STATUSIN;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Report size of requested data */
|
||||
if(m_cmd[0] == CMD_MODE_SENSE6) {
|
||||
m_responsebuffer[0] = len;
|
||||
} else {
|
||||
m_responsebuffer[0] = (len >> 8) & 0xff;
|
||||
m_responsebuffer[1] = len & 0xff;
|
||||
}
|
||||
|
||||
/* Truncate data if necessary */
|
||||
if (maxlen < len) {
|
||||
len = maxlen;
|
||||
}
|
||||
|
||||
// Send it
|
||||
writeDataPhase(len, m_responsebuffer);
|
||||
}
|
||||
m_phase = PHASE_STATUSIN;
|
||||
}
|
||||
|
||||
uint8_t onModeSelectCommand() {
|
||||
m_sense[m_id][m_lun].m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
|
||||
m_sense[m_id][m_lun].m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
|
||||
m_sense[m_id][m_lun].m_key_specific[1] = 0x00;
|
||||
m_sense[m_id][m_lun].m_key_specific[2] = 0x04;
|
||||
m_sel->m_sense.m_code = INVALID_FIELD_IN_CDB; /* "Invalid field in CDB" */
|
||||
m_sel->m_sense.m_key_specific[0] = ERROR_IN_OPCODE; /* "Error in Byte 2" */
|
||||
m_sel->m_sense.m_key_specific[1] = 0x00;
|
||||
m_sel->m_sense.m_key_specific[2] = 0x04;
|
||||
return 0x02;
|
||||
}
|
||||
|
||||
void ModeSelect6CommandHandler() {
|
||||
LOG("[ModeSelect6] ");
|
||||
uint16_t len = m_cmd[4];
|
||||
readDataPhase(len, m_buf);
|
||||
readDataPhase(len, m_responsebuffer);
|
||||
|
||||
for(int i = 1; i < len; i++ ) {
|
||||
LOG(":");
|
||||
LOGHEX2(m_buf[i]);
|
||||
LOGHEX2(m_responsebuffer[i]);
|
||||
}
|
||||
LOGN("");
|
||||
// m_sts |= onModeSelectCommand();
|
||||
@ -99,11 +374,11 @@ void ModeSelect6CommandHandler() {
|
||||
void ModeSelect10CommandHandler() {
|
||||
LOGN("[ModeSelect10]");
|
||||
uint16_t len = ((uint16_t)m_cmd[7] << 8) | m_cmd[8];
|
||||
readDataPhase(len, m_buf);
|
||||
readDataPhase(len, m_responsebuffer);
|
||||
|
||||
for(int i = 1; i < len; i++ ) {
|
||||
LOG(":");
|
||||
LOGHEX2(m_buf[i]);
|
||||
LOGHEX2(m_responsebuffer[i]);
|
||||
}
|
||||
LOGN("");
|
||||
// m_sts |= onModeSelectCommand();
|
||||
@ -120,16 +395,10 @@ void PreAllowMediumRemovalCommandHandler() {
|
||||
m_phase = PHASE_STATUSIN;
|
||||
}
|
||||
|
||||
void ModeSense10CommandHandler() {
|
||||
LOGN("[ModeSense10]");
|
||||
onModeSenseCommand(m_cmd[1] & 0x80, m_cmd[2], ((uint32_t)m_cmd[7] << 8) | m_cmd[8]);
|
||||
m_phase = PHASE_STATUSIN;
|
||||
}
|
||||
|
||||
void UnknownCommandHandler() {
|
||||
LOGN("[*Unknown]");
|
||||
m_sts |= 0x02;
|
||||
m_sense[m_id][m_lun].m_key = 5;
|
||||
m_sel->m_sense.m_key = 5;
|
||||
m_phase = PHASE_STATUSIN;
|
||||
}
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
893
src/greenscsi.ino
Executable file
893
src/greenscsi.ino
Executable file
@ -0,0 +1,893 @@
|
||||
/*
|
||||
* GreenSCSI
|
||||
* Copyright (c) 2021 David Kuder
|
||||
*
|
||||
* Based on BlueSCSI
|
||||
* Copyright (c) 2021 Eric Helgeson, Androda
|
||||
*
|
||||
* This file is free software: you may copy, redistribute and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* Free Software Foundation, either version 2 of the License, or (at your
|
||||
* option) any later version.
|
||||
*
|
||||
* This file is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see https://github.com/erichelgeson/bluescsi.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and
|
||||
* permission notice:
|
||||
*
|
||||
* Copyright (c) 2019 komatsu
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software
|
||||
* for any purpose with or without fee is hereby granted, provided
|
||||
* that the above copyright notice and this permission notice appear
|
||||
* in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
||||
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
||||
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
|
||||
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
||||
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <Arduino.h> // For Platform.IO
|
||||
#include <SPI.h>
|
||||
#include <SdFat.h>
|
||||
#include "sdios.h"
|
||||
#include "config.h"
|
||||
#include "scsi_defs.h"
|
||||
#include "cmd.h"
|
||||
|
||||
// SDFAT
|
||||
SdFs sd;
|
||||
|
||||
#if DEBUG == 1
|
||||
#define LOG(XX) Serial.print(XX)
|
||||
#define LOGHEX2(XX) Serial.printf("%02x", XX)
|
||||
#define LOGHEX4(XX) Serial.printf("%04x", XX)
|
||||
#define LOGHEX6(XX) Serial.printf("%06x", XX)
|
||||
#define LOGHEX8(XX) Serial.printf("%08x", XX)
|
||||
#define LOGN(XX) Serial.println(XX)
|
||||
#define LOGHEX2N(XX) Serial.printf("%02x\r\n", XX)
|
||||
#define LOGHEX4N(XX) Serial.printf("%04x\r\n", XX)
|
||||
#define LOGHEX6N(XX) Serial.printf("%06x\r\n", XX)
|
||||
#define LOGHEX8N(XX) Serial.printf("%08x\r\n", XX)
|
||||
#elif DEBUG == 2
|
||||
#define LOG(XX) LOG_FILE.print(XX); LOG_FILE.sync();
|
||||
#define LOGHEX2(XX) LOG_FILE.printf("%02x", XX); LOG_FILE.sync();
|
||||
#define LOGHEX4(XX) LOG_FILE.printf("%04x", XX); LOG_FILE.sync();
|
||||
#define LOGHEX6(XX) LOG_FILE.printf("%06x", XX); LOG_FILE.sync();
|
||||
#define LOGHEX8(XX) LOG_FILE.printf("%08x", XX); LOG_FILE.sync();
|
||||
#define LOGN(XX) LOG_FILE.println(XX); LOG_FILE.sync();
|
||||
#define LOGHEX2N(XX) LOG_FILE.printf("%02x\r\n", XX); LOG_FILE.sync();
|
||||
#define LOGHEX4N(XX) LOG_FILE.printf("%04x\r\n", XX); LOG_FILE.sync();
|
||||
#define LOGHEX6N(XX) LOG_FILE.printf("%06x\r\n", XX); LOG_FILE.sync();
|
||||
#define LOGHEX8N(XX) LOG_FILE.printf("%08x\r\n", XX); LOG_FILE.sync();
|
||||
#else
|
||||
#define LOG(XX) //Serial.print(XX)
|
||||
#define LOGHEX2(XX) //Serial.printf("%02x", XX)
|
||||
#define LOGHEX4(XX) //Serial.printf("%04x", XX)
|
||||
#define LOGHEX6(XX) //Serial.printf("%06x", XX)
|
||||
#define LOGHEX8(XX) //Serial.printf("%08x", XX)
|
||||
#define LOGN(XX) //Serial.println(XX)
|
||||
#define LOGHEX2N(XX) //Serial.printf("%02x\r\n", XX)
|
||||
#define LOGHEX4N(XX) //Serial.printf("%04x\r\n", XX)
|
||||
#define LOGHEX6N(XX) //Serial.printf("%06x\r\n", XX)
|
||||
#define LOGHEX8N(XX) //Serial.printf("%08x\r\n", XX)
|
||||
#endif
|
||||
|
||||
#define DB0 0 // SCSI:DB0 Port B Bit 16
|
||||
#define DB1 1 // SCSI:DB1 Port B Bit 17
|
||||
#define DB2 29 // SCSI:DB2 Port B Bit 18
|
||||
#define DB3 30 // SCSI:DB3 Port B Bit 19
|
||||
#define DB4 43 // SCSI:DB4 Port B Bit 20
|
||||
#define DB5 46 // SCSI:DB5 Port B Bit 21
|
||||
#define DB6 44 // SCSI:DB6 Port B Bit 22
|
||||
#define DB7 45 // SCSI:DB7 Port B Bit 23
|
||||
#define DB8 32 // SCSI:DBP Port B Bit 11
|
||||
|
||||
#define ATN 12 // SCSI:ATN Port C Bit 7
|
||||
#define BSY 10 // SCSI:BSY Port C Bit 4
|
||||
#define ACK 9 // SCSI:ACK Port C Bit 3
|
||||
#define RST 8 // SCSI:RST Port D Bit 3
|
||||
#define MSG 7 // SCSI:MSG Port D Bit 2
|
||||
#define SEL 6 // SCSI:SEL Port D Bit 4
|
||||
#define CD 5 // SCSI:C/D Port D Bit 7
|
||||
#define REQ 4 // SCSI:REQ Port A Bit 13
|
||||
#define IO 3 // SCSI:I/O Port A Bit 12
|
||||
|
||||
#define SD_CS BUILTIN_SDCARD // SDCARD:CS
|
||||
#define LED 13 // LED Port C Bit 5
|
||||
|
||||
// LED control
|
||||
#define LED_ON() { GPIOC_PSOR = (1 << 5); }
|
||||
#define LED_OFF() { GPIOC_PCOR = (1 << 5); }
|
||||
|
||||
#define SET_REQ_ACTIVE() { GPIOA_PCOR = (1 << 13); }
|
||||
#define SET_REQ_INACTIVE() { GPIOA_PSOR = (1 << 13); }
|
||||
#define SET_MSG_ACTIVE() { GPIOD_PCOR = (1 << 2); }
|
||||
#define SET_MSG_INACTIVE() { GPIOD_PSOR = (1 << 2); }
|
||||
//#define SET_CD_ACTIVE() { GPIOD_PCOR = (1 << 7); }
|
||||
//#define SET_CD_INACTIVE() { GPIOD_PSOR = (1 << 7); }
|
||||
//#define SET_IO_ACTIVE() { GPIOA_PCOR = (1 << 12); }
|
||||
//#define SET_IO_INACTIVE() { GPIOA_PSOR = (1 << 12); }
|
||||
|
||||
//#define SET_REQ_ACTIVE() { pinMode(REQ, OUTPUT_OPENDRAIN); digitalWrite(REQ, LOW); }
|
||||
//#define SET_REQ_INACTIVE() { digitalWrite(REQ, HIGH); pinMode(REQ, INPUT); }
|
||||
//#define SET_MSG_ACTIVE() { pinMode(MSG, OUTPUT_OPENDRAIN); digitalWrite(MSG, LOW); }
|
||||
//#define SET_MSG_INACTIVE() { digitalWrite(MSG, HIGH); pinMode(MSG, INPUT); }
|
||||
#define SET_CD_ACTIVE() { pinMode(CD, OUTPUT_OPENDRAIN); digitalWrite(CD, LOW); }
|
||||
#define SET_CD_INACTIVE() { digitalWrite(CD, HIGH); pinMode(CD, INPUT); }
|
||||
#define SET_IO_ACTIVE() { pinMode(IO, OUTPUT_OPENDRAIN); digitalWrite(IO, LOW); }
|
||||
#define SET_IO_INACTIVE() { digitalWrite(IO, HIGH); pinMode(IO, INPUT); }
|
||||
//#define SET_BSY_ACTIVE() { pinMode(BSY, OUTPUT_OPENDRAIN); digitalWrite(BSY, LOW); }
|
||||
//#define SET_BSY_INACTIVE() { digitalWrite(BSY, HIGH); pinMode(BSY, INPUT); }
|
||||
|
||||
#define SET_BSY_ACTIVE() { GPIOC_PCOR = (1 << 4); }
|
||||
#define SET_BSY_INACTIVE() { GPIOC_PSOR = (1 << 4); }
|
||||
|
||||
#define GET_ACK() (!(GPIOC_PDIR & (1 << 3)))
|
||||
#define GET_ATN() (!(GPIOC_PDIR & (1 << 7)))
|
||||
#define GET_BSY() (!(GPIOC_PDIR & (1 << 4)))
|
||||
#define GET_RST() (!(GPIOD_PDIR & (1 << 3)))
|
||||
#define GET_SEL() (!(GPIOD_PDIR & (1 << 4)))
|
||||
|
||||
// Turn on the output only for BSY
|
||||
// BSY,REQ,MSG,CD,IO Turn on the output (no change required for OD)
|
||||
#define SCSI_TARGET_ACTIVE() { }
|
||||
// BSY,REQ,MSG,CD,IO Turn off output
|
||||
#define SCSI_TARGET_INACTIVE() { SCSI_DB_INPUT(); SET_REQ_INACTIVE(); SET_MSG_INACTIVE(); SET_CD_INACTIVE(); SET_IO_INACTIVE(); SET_BSY_INACTIVE(); }
|
||||
|
||||
// HDImage file
|
||||
#define HDIMG_ID_POS 2 // Position to embed ID number
|
||||
#define HDIMG_LUN_POS 3 // Position to embed LUN numbers
|
||||
#define HDIMG_BLK_POS 5 // Position to embed block size numbers
|
||||
#define MAX_FILE_PATH 32 // Maximum file name length
|
||||
|
||||
typedef struct m_sense_s {
|
||||
uint8_t m_key;
|
||||
uint8_t m_code;
|
||||
uint8_t m_key_specific[4];
|
||||
} msense_t;
|
||||
|
||||
typedef void (*CommandHandler_t)();
|
||||
CommandHandler_t m_badlunhandler[256];
|
||||
|
||||
// VirtualDevice
|
||||
typedef struct VirtualDevice_s
|
||||
{
|
||||
boolean m_enabled;
|
||||
uint8_t m_id;
|
||||
uint8_t m_lun;
|
||||
uint8_t m_type; // Device Type
|
||||
char m_filename[MAX_FILE_PATH+1];
|
||||
FsFile m_file; // File object
|
||||
uint64_t m_fileSize; // File size
|
||||
size_t m_blocksize; // SCSI BLOCK size
|
||||
#if SUPPORT_TAPE
|
||||
size_t m_filemarks; // Tape position counter (file marks since BOM)
|
||||
#endif
|
||||
uint8_t m_inquiryresponse[96];
|
||||
CommandHandler_t m_handler[256];
|
||||
msense_t m_sense;
|
||||
} VirtualDevice_t;
|
||||
|
||||
VirtualDevice_t m_vdev[NUM_VDEV]; // Maximum number
|
||||
uint8_t m_vdevcnt = 0; // Number of allocated vdevs
|
||||
uint8_t m_vdevmap[NUM_SCSIID][NUM_SCSILUN]; // Map ID/LUN to a vdev
|
||||
|
||||
volatile bool m_isBusReset = false; // Bus reset
|
||||
|
||||
uint8_t scsi_id_mask; // Mask list of responding SCSI IDs
|
||||
uint8_t m_id; // Currently responding SCSI-ID
|
||||
uint8_t m_lun; // Logical unit number currently responding
|
||||
uint8_t m_sts; // Status uint8_t
|
||||
uint8_t m_msg; // Message uint8_ts
|
||||
VirtualDevice_t *m_sel; // VirtualDevice for current SCSI-ID, LUN
|
||||
uint8_t m_buf[MAX_BLOCKSIZE+1]; // General purpose buffer + overrun fetch
|
||||
int m_msc;
|
||||
uint8_t m_msb[256]; // Command storage uint8_ts
|
||||
uint8_t m_cmdlen;
|
||||
uint8_t m_cmd[12];
|
||||
uint8_t m_responsebuffer[256];
|
||||
#if SUPPORT_SASI
|
||||
boolean m_sasi_mode = SUPPORT_SASI_DEFAULT;
|
||||
#endif
|
||||
|
||||
uint16_t ledbits = 0;
|
||||
uint8_t ledbit = 0;
|
||||
|
||||
typedef enum {
|
||||
PHASE_BUSFREE = 0,
|
||||
PHASE_SELECTION,
|
||||
PHASE_MESSAGEOUT,
|
||||
PHASE_COMMAND,
|
||||
PHASE_STATUSIN,
|
||||
PHASE_MESSAGEIN,
|
||||
} phase_t;
|
||||
phase_t m_phase = PHASE_BUSFREE;
|
||||
|
||||
// Log File
|
||||
#define VERSION "1.2-20211126"
|
||||
#if DEBUG == 2
|
||||
#define LOG_FILENAME "LOG.txt"
|
||||
FsFile LOG_FILE;
|
||||
#endif
|
||||
|
||||
|
||||
void onBusReset(void);
|
||||
void initFileLog(void);
|
||||
void finalizeFileLog(void);
|
||||
|
||||
/*
|
||||
* IO read.
|
||||
*/
|
||||
inline uint8_t readIO(void)
|
||||
{
|
||||
// Port input data register
|
||||
uint32_t ret = ~GPIOB_PDIR;
|
||||
//uint8_t bret = ret >> 16;
|
||||
#if READ_PARITY_CHECK
|
||||
if((db_bsrr[bret] ^ ret) & 0x0800)
|
||||
m_sts |= 0x01; // parity error
|
||||
#endif
|
||||
|
||||
return ret >> 16; //bret;
|
||||
}
|
||||
|
||||
boolean OpenImage(VirtualDevice_t *h, const char *image_name)
|
||||
{
|
||||
h->m_fileSize = 0;
|
||||
h->m_file = sd.open(image_name, O_RDWR);
|
||||
if(h->m_file.isOpen()) {
|
||||
h->m_fileSize = h->m_file.size();
|
||||
return true; // File opened
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#if SUPPORT_DISK or SUPPORT_OPTICAL
|
||||
/*
|
||||
* Open HDD or CDROM image file
|
||||
*/
|
||||
boolean OpenDiskImage(VirtualDevice_t *h, const char *image_name, int blocksize)
|
||||
{
|
||||
if(!strncmp(image_name, "/sd/", 4))
|
||||
image_name += 3;
|
||||
|
||||
h->m_fileSize = 0;
|
||||
h->m_blocksize = blocksize;
|
||||
h->m_file = sd.open(image_name, O_RDWR);
|
||||
if(h->m_file.isOpen())
|
||||
{
|
||||
h->m_fileSize = h->m_file.size();
|
||||
LOG(" Imagefile: ");
|
||||
LOG(image_name);
|
||||
if(h->m_fileSize>0)
|
||||
{
|
||||
// check blocksize dummy file
|
||||
LOG(" / ");
|
||||
LOG(h->m_fileSize / h->m_blocksize);
|
||||
LOG(" sectors / ");
|
||||
LOG(h->m_fileSize / 1024);
|
||||
LOG(" KiB / ");
|
||||
LOG(h->m_fileSize / 1024 / 1024);
|
||||
LOGN(" MiB");
|
||||
return true; // File opened
|
||||
}
|
||||
else
|
||||
{
|
||||
h->m_file.close();
|
||||
h->m_fileSize = h->m_blocksize = 0; // no file
|
||||
LOGN(" FileSizeError");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif /* SUPPORT_DISK or SUPPORT_OPTICAL */
|
||||
|
||||
#if SUPPORT_TAPE
|
||||
/*
|
||||
* Open Tape image file
|
||||
*/
|
||||
boolean OpenTapeImage(VirtualDevice_t *h, const char *image_name)
|
||||
{
|
||||
h->m_fileSize = 0;
|
||||
h->m_blocksize = 0;
|
||||
h->m_filemarks = 0;
|
||||
h->m_file = sd.open(image_name, O_RDWR);
|
||||
if(h->m_file.isOpen()) {
|
||||
h->m_fileSize = h->m_file.size();
|
||||
h->m_blocksize = 1024;
|
||||
LOG(" Imagefile: ");
|
||||
LOG(image_name);
|
||||
if(h->m_fileSize>0) {
|
||||
// check blocksize dummy file
|
||||
LOG(" / ");
|
||||
LOG(h->m_fileSize / 1024);
|
||||
LOG(" KiB / ");
|
||||
LOG(h->m_fileSize / 1024 / 1024);
|
||||
LOGN(" MiB");
|
||||
} else {
|
||||
LOGN(" / Blank Tape");
|
||||
}
|
||||
return true; // File opened
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif /* SUPPORT_TAPE */
|
||||
|
||||
void pinModeFastSlew(uint8_t pin, int dummy) {
|
||||
pinMode(pin, OUTPUT_OPENDRAIN);
|
||||
volatile uint32_t *config = portConfigRegister(pin);
|
||||
*config = PORT_PCR_ODE | PORT_PCR_DSE | PORT_PCR_MUX(1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialization.
|
||||
* Initialize the bus and set the PIN orientation
|
||||
*/
|
||||
void setup()
|
||||
{
|
||||
#if DEBUG > 0
|
||||
Serial.begin(115200);
|
||||
|
||||
delay(1500);
|
||||
#endif
|
||||
|
||||
for(int id = 0; id < NUM_SCSIID; id++)
|
||||
for(int lun = 0; lun < NUM_SCSILUN; lun++)
|
||||
m_vdevmap[id][lun] = 0xff;
|
||||
m_vdevcnt = 0;
|
||||
|
||||
// PIN initialization
|
||||
pinMode(LED, OUTPUT);
|
||||
LED_OFF();
|
||||
|
||||
//GPIO(SCSI BUS)Initialization
|
||||
|
||||
// Input port
|
||||
pinMode(ATN, INPUT_PULLUP);
|
||||
pinMode(ACK, INPUT_PULLUP);
|
||||
pinMode(RST, INPUT_PULLUP);
|
||||
pinMode(SEL, INPUT_PULLUP);
|
||||
|
||||
// Output port
|
||||
pinModeFastSlew(BSY, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(MSG, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(CD, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(IO, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(REQ, OUTPUT_OPENDRAIN);
|
||||
|
||||
pinModeFastSlew(DB0, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(DB1, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(DB2, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(DB3, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(DB4, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(DB5, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(DB6, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(DB7, OUTPUT_OPENDRAIN);
|
||||
pinModeFastSlew(DB8, OUTPUT_OPENDRAIN);
|
||||
|
||||
// DB and DP are input modes
|
||||
SCSI_DB_INPUT();
|
||||
|
||||
// Turn off the output port
|
||||
SCSI_TARGET_INACTIVE();
|
||||
|
||||
//Occurs when the RST pin state changes from HIGH to LOW
|
||||
attachInterrupt(RST, onBusReset, FALLING);
|
||||
attachInterrupt(SEL, SelectionPhaseISR, FALLING);
|
||||
|
||||
LED_ON();
|
||||
|
||||
if(!sd.begin(SdioConfig(FIFO_SDIO))) {
|
||||
#if DEBUG > 0
|
||||
Serial.println("SD initialization failed!");
|
||||
#endif
|
||||
ledbits = 0b0000001010101010;
|
||||
}
|
||||
initFileLog();
|
||||
ConfigureBadLunHandlers();
|
||||
|
||||
//Sector data overrun uint8_t setting
|
||||
//m_buf[MAX_BLOCKSIZE] = 0xff; // DB0 all off,DBP off
|
||||
//HD image file open
|
||||
scsi_id_mask = 0x00;
|
||||
|
||||
// If greenscsi.cfg exists, run it
|
||||
if(sd.exists("/greenscsi.cfg"))
|
||||
execHandler((char*)"/sd/greenscsi.cfg");
|
||||
|
||||
// Scan for images if we haven't defined any targets yet.
|
||||
if(m_vdevcnt == 0) findImages();
|
||||
|
||||
finalizeFileLog();
|
||||
LED_OFF();
|
||||
|
||||
cmdDisplay();
|
||||
}
|
||||
|
||||
void findImages() {
|
||||
// Iterate over the root path in the SD card looking for candidate image files.
|
||||
SdFile root;
|
||||
root.open("/");
|
||||
SdFile file;
|
||||
bool imageReady;
|
||||
int usedDefaultId = 0;
|
||||
while (1) {
|
||||
if (!file.openNext(&root, O_READ)) break;
|
||||
char name[MAX_FILE_PATH+1];
|
||||
if(!file.isDir()) {
|
||||
file.getName(name, MAX_FILE_PATH+1);
|
||||
file.close();
|
||||
String file_name = String(name);
|
||||
file_name.toLowerCase();
|
||||
|
||||
#if SUPPORT_DISK or SUPPORT_OPTICAL
|
||||
if((file_name.startsWith("hd") || file_name.startsWith("cd")) && (file_name.endsWith(".img") || file_name.endsWith(".hda"))) {
|
||||
// Defaults for Hard Disks and CD-ROMs
|
||||
int id = 1; // 0 and 3 are common in Macs for physical HD and CD, so avoid them.
|
||||
int lun = 0;
|
||||
int blk = 512;
|
||||
|
||||
// Positionally read in and coerase the chars to integers.
|
||||
// We only require the minimum and read in the next if provided.
|
||||
int file_name_length = file_name.length();
|
||||
if(file_name_length > 2) { // HD[N]
|
||||
int tmp_id = file_name[HDIMG_ID_POS] - '0';
|
||||
|
||||
if(tmp_id > -1 && tmp_id < NUM_SCSIID) {
|
||||
id = tmp_id; // If valid id, set it, else use default
|
||||
} else {
|
||||
usedDefaultId++;
|
||||
}
|
||||
}
|
||||
if(file_name_length > 3) { // HD0[N]
|
||||
int tmp_lun = file_name[HDIMG_LUN_POS] - '0';
|
||||
|
||||
if(tmp_lun > -1 && tmp_lun < NUM_SCSILUN) {
|
||||
lun = tmp_lun; // If valid id, set it, else use default
|
||||
}
|
||||
}
|
||||
|
||||
int blk1 = 0, blk2 = 0, blk3 = 0, blk4 = 0;
|
||||
if(file_name_length > 8) { // HD00_[111]
|
||||
blk1 = name[HDIMG_BLK_POS] - '0';
|
||||
blk2 = name[HDIMG_BLK_POS+1] - '0';
|
||||
blk3 = name[HDIMG_BLK_POS+2] - '0';
|
||||
if(file_name_length > 9) // HD00_NNN[1]
|
||||
blk4 = name[HDIMG_BLK_POS+3] - '0';
|
||||
}
|
||||
if(blk1 == 2 && blk2 == 5 && blk3 == 6) {
|
||||
blk = 256;
|
||||
} else if(blk1 == 1 && blk2 == 0 && blk3 == 2 && blk4 == 4) {
|
||||
blk = 1024;
|
||||
} else if(blk1 == 2 && blk2 == 0 && blk3 == 4 && blk4 == 8) {
|
||||
blk = 2048;
|
||||
}
|
||||
|
||||
if(id < NUM_SCSIID && lun < NUM_SCSILUN) {
|
||||
uint8_t v = m_vdevcnt;
|
||||
VirtualDevice_t *h = &m_vdev[v];
|
||||
m_vdevmap[id][lun] = v;
|
||||
h->m_id = id;
|
||||
h->m_lun = lun;
|
||||
imageReady = false;
|
||||
|
||||
// Configure Handlers and Inquiry Response for all luns
|
||||
#if SUPPORT_DISK
|
||||
if(file_name.startsWith("hd")) {
|
||||
LOG(" Disk at ID ");
|
||||
LOG(id);
|
||||
LOG(":LUN ");
|
||||
LOG(lun);
|
||||
ConfigureDisk(h, name);
|
||||
imageReady = OpenDiskImage(h, name, blk);
|
||||
}
|
||||
#endif /* SUPPORT_DISK */
|
||||
#if SUPPORT_OPTICAL
|
||||
if(file_name.startsWith("cd")) {
|
||||
LOG("CDROM at ID ");
|
||||
LOG(id);
|
||||
LOG(":LUN ");
|
||||
LOG(lun);
|
||||
ConfigureOptical(h, name);
|
||||
imageReady = OpenDiskImage(h, name, blk);
|
||||
}
|
||||
#endif /* SUPPORT_OPTICAL */
|
||||
if(imageReady) { // Marked as a responsive ID
|
||||
m_vdevcnt++;
|
||||
sprintf(h->m_filename, "/sd/%s", name);
|
||||
h->m_enabled = true;
|
||||
scsi_id_mask |= 1<<id;
|
||||
}
|
||||
} else {
|
||||
LOG("Bad LUN or SCSI id for image: ");
|
||||
LOGN(name);
|
||||
}
|
||||
} else
|
||||
#endif /* SUPPORT_DISK or SUPPORT_OPTICAL */
|
||||
#if SUPPORT_TAPE
|
||||
if(file_name.startsWith("dt") && file_name.endsWith(".tap")) {
|
||||
// Defaults for Tapes
|
||||
int id = 1; // 0 and 3 are common in Macs for physical HD and CD, so avoid them.
|
||||
int lun = 0;
|
||||
|
||||
// Positionally read in and coerase the chars to integers.
|
||||
// We only require the minimum and read in the next if provided.
|
||||
int file_name_length = file_name.length();
|
||||
if(file_name_length > 2) { // HD[N]
|
||||
int tmp_id = file_name[HDIMG_ID_POS] - '0';
|
||||
|
||||
if(tmp_id > -1 && tmp_id < NUM_SCSIID) {
|
||||
id = tmp_id; // If valid id, set it, else use default
|
||||
} else {
|
||||
usedDefaultId++;
|
||||
}
|
||||
}
|
||||
if(file_name_length > 3) { // HD0[N]
|
||||
int tmp_lun = file_name[HDIMG_LUN_POS] - '0';
|
||||
|
||||
if(tmp_lun > -1 && tmp_lun < NUM_SCSILUN) {
|
||||
lun = tmp_lun; // If valid id, set it, else use default
|
||||
}
|
||||
}
|
||||
|
||||
if(id < NUM_SCSIID && lun < NUM_SCSILUN) {
|
||||
uint8_t v = m_vdevcnt;
|
||||
VirtualDevice_t *h = &m_vdev[v];
|
||||
m_vdevmap[id][lun] = v;
|
||||
h->m_id = id;
|
||||
h->m_lun = lun;
|
||||
imageReady = false;
|
||||
|
||||
LOG(" Tape at ID ");
|
||||
LOG(id);
|
||||
LOG(":LUN ");
|
||||
LOG(lun);
|
||||
ConfigureTape(h, name);
|
||||
imageReady = OpenTapeImage(h, name);
|
||||
if(imageReady) { // Marked as a responsive ID
|
||||
m_vdevcnt++;
|
||||
h->m_enabled = true;
|
||||
sprintf(h->m_filename, "/sd/%s", name);
|
||||
scsi_id_mask |= 1<<id;
|
||||
}
|
||||
} else {
|
||||
LOG("Bad LUN or SCSI id for image: ");
|
||||
LOGN(name);
|
||||
}
|
||||
} else
|
||||
#endif /* SUPPORT_TAPE */
|
||||
{
|
||||
// LOG("Not an image: ");
|
||||
// LOGN(name);
|
||||
}
|
||||
} else {
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
if(usedDefaultId > 0) {
|
||||
LOGN("!! More than one image did not specify a SCSI ID. Last file will be used at ID 1. !!");
|
||||
}
|
||||
if(m_vdevcnt == 0) {
|
||||
LOGN("!! No Virtual Devices defined. Please manually configure. !!");
|
||||
}
|
||||
root.close();
|
||||
}
|
||||
|
||||
/*
|
||||
* Setup initialization logfile
|
||||
*/
|
||||
void initFileLog() {
|
||||
#if DEBUG == 2
|
||||
LOG_FILE = sd.open(LOG_FILENAME, O_WRONLY | O_CREAT | O_TRUNC);
|
||||
#endif
|
||||
#if DEBUG
|
||||
LOGN("GreenSCSI <-> SD - https://github.com/dkgrizzly/GreenSCSI");
|
||||
LOG("VERSION: ");
|
||||
LOGN(VERSION);
|
||||
LOG("DEBUG:");
|
||||
LOGN(DEBUG);
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
* Finalize initialization logfile
|
||||
*/
|
||||
void finalizeFileLog() {
|
||||
LOGN("Initialization complete.");
|
||||
#if DEBUG == 2
|
||||
LOG_FILE.close();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* MsgIn2.
|
||||
*/
|
||||
void MsgIn2(int msg)
|
||||
{
|
||||
//LOGN("MsgIn2");
|
||||
SET_MSG_ACTIVE();
|
||||
SET_CD_ACTIVE();
|
||||
SET_IO_ACTIVE();
|
||||
writeHandshake(msg);
|
||||
}
|
||||
|
||||
/*
|
||||
* MsgOut2.
|
||||
*/
|
||||
void MsgOut2()
|
||||
{
|
||||
//LOGN("MsgOut2");
|
||||
SET_MSG_ACTIVE();
|
||||
SET_CD_ACTIVE();
|
||||
SET_IO_INACTIVE();
|
||||
m_msb[m_msc] = readHandshake();
|
||||
m_msc++;
|
||||
m_msc %= 256;
|
||||
}
|
||||
|
||||
/*
|
||||
* Main loop.
|
||||
*/
|
||||
elapsedMillis ticktock;
|
||||
|
||||
void loop() {
|
||||
if(ledbits) {
|
||||
if(ticktock > 250) {
|
||||
if(ledbits & (1<<ledbit)) {
|
||||
LED_ON();
|
||||
} else {
|
||||
LED_OFF();
|
||||
}
|
||||
|
||||
ledbit++;
|
||||
ledbit &= 0xf;
|
||||
|
||||
ticktock = 0;
|
||||
}
|
||||
|
||||
// Cancel ID error indication if we have any IDs enabled
|
||||
if((ledbit == 0) && (ledbits == 0b0000000000101010) && (scsi_id_mask != 0))
|
||||
ledbits = 0;
|
||||
}
|
||||
if(!ledbits) {
|
||||
if(scsi_id_mask == 0) ledbits = 0b0000000000101010;
|
||||
}
|
||||
|
||||
switch(m_phase) {
|
||||
default:
|
||||
case PHASE_BUSFREE:
|
||||
BusFreePhaseHandler();
|
||||
break;
|
||||
case PHASE_SELECTION:
|
||||
//SelectionPhaseHandler();
|
||||
|
||||
// Do other work while we wait for SEL to be asserted
|
||||
cmdPoll();
|
||||
break;
|
||||
case PHASE_MESSAGEOUT:
|
||||
MessageOutPhaseHandler();
|
||||
break;
|
||||
case PHASE_COMMAND:
|
||||
CommandPhaseHandler();
|
||||
break;
|
||||
case PHASE_STATUSIN:
|
||||
StatusInPhaseHandler();
|
||||
break;
|
||||
case PHASE_MESSAGEIN:
|
||||
MessageInPhaseHandler();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SelectionPhaseISR() {
|
||||
m_msg = 0;
|
||||
|
||||
// Wait until RST = H, BSY = H, SEL = L
|
||||
while(GET_BSY() || GET_RST()) {
|
||||
if(!GET_SEL()) return;
|
||||
}
|
||||
|
||||
// BSY+ SEL-
|
||||
// If the ID to respond is not driven, wait for the next
|
||||
uint8_t scsiid = readIO() & scsi_id_mask;
|
||||
if(scsiid == 0) {
|
||||
// We were not selected, treat this as an abort and cancel any transactions we can.
|
||||
m_isBusReset = true;
|
||||
SCSI_TARGET_INACTIVE();
|
||||
m_phase = PHASE_BUSFREE;
|
||||
return;
|
||||
}
|
||||
LOG("Selection ");
|
||||
m_isBusReset = false;
|
||||
// Set BSY to-when selected
|
||||
SET_BSY_ACTIVE(); // Turn only BSY output ON, ACTIVE
|
||||
|
||||
// Ask for a TARGET-ID to respond
|
||||
#if USE_DB2ID_TABLE
|
||||
m_id = db2scsiid[scsiid];
|
||||
#else
|
||||
for(m_id=7;m_id>=0;m_id--)
|
||||
if(scsiid & (1<<m_id)) break;
|
||||
#endif
|
||||
m_lun = 0xff;
|
||||
|
||||
//LOGN("Wait !SEL");
|
||||
// Wait until SEL becomes inactive
|
||||
while(GET_SEL() && !GET_BSY()) {
|
||||
if(m_isBusReset) {
|
||||
LOGN("!SEL");
|
||||
m_phase = PHASE_BUSFREE;
|
||||
return;
|
||||
}
|
||||
}
|
||||
m_phase = PHASE_MESSAGEOUT;
|
||||
}
|
||||
|
||||
void MessageOutPhaseHandler() {
|
||||
//LOGN("ACTIVE");
|
||||
SCSI_TARGET_ACTIVE(); // (BSY), REQ, MSG, CD, IO output turned on
|
||||
//
|
||||
if(GET_ATN()) {
|
||||