File size: 4,272 Bytes
c7436b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef COMMAND_INTF_H
#define COMMAND_INTF_H

#include "integer.h"
#include "config.h"
#include "iomap.h"
#include "subsys.h"

#define CMD_IF_RAM_BASE  (CMD_IF_BASE + 0x800)

#define CMD_IF_RAM(x)          *((volatile uint8_t *)(CMD_IF_RAM_BASE +x))

#ifndef CMD_IF_SLOT_BASE
#define CMD_IF_SLOT_BASE       *((volatile uint8_t *)(CMD_IF_BASE + 0x0))
#define CMD_IF_SLOT_ENABLE     *((volatile uint8_t *)(CMD_IF_BASE + 0x1))
#endif
#define CMD_IF_HANDSHAKE_OUT   *((volatile uint8_t *)(CMD_IF_BASE + 0x2))
#define CMD_IF_STATUSBYTE      *((volatile uint8_t *)(CMD_IF_BASE + 0x3))
#define CMD_IF_COMMAND_START   *((volatile uint8_t *)(CMD_IF_BASE + 0x4))
#define CMD_IF_COMMAND_END     *((volatile uint8_t *)(CMD_IF_BASE + 0x5))
#define CMD_IF_RESPONSE_START  *((volatile uint8_t *)(CMD_IF_BASE + 0x6))
#define CMD_IF_RESPONSE_END    *((volatile uint8_t *)(CMD_IF_BASE + 0x7))
#define CMD_IF_STATUS_START    *((volatile uint8_t *)(CMD_IF_BASE + 0x8))
#define CMD_IF_STATUS_END      *((volatile uint8_t *)(CMD_IF_BASE + 0x9))
#define CMD_IF_STATUS_LENGTH   *((volatile uint8_t *)(CMD_IF_BASE + 0xA))
#define CMD_IF_IRQMASK         *((volatile uint8_t *)(CMD_IF_BASE + 0xB))
#define CMD_IF_RESPONSE_LEN_L  *((volatile uint8_t *)(CMD_IF_BASE + 0xC))
#define CMD_IF_RESPONSE_LEN_H  *((volatile uint8_t *)(CMD_IF_BASE + 0xD))
#define CMD_IF_COMMAND_LEN_L   *((volatile uint8_t *)(CMD_IF_BASE + 0xE))
#define CMD_IF_COMMAND_LEN_H   *((volatile uint8_t *)(CMD_IF_BASE + 0xF))
#define CMD_IF_IRQMASK_SET     *((volatile uint8_t *)(CMD_IF_BASE + 0x4))
#define CMD_IF_IRQMASK_CLEAR   *((volatile uint8_t *)(CMD_IF_BASE + 0x5))

#define CMD_NEW_COMMAND           0x01
#define CMD_DATA_ACCEPTED         0x02
#define CMD_ABORT_DATA            0x04
#define CMD_MORE_OR_ABORT         0x06
#define CMD_STATE_BITS            0x30
#define CMD_STATE_IDLE            0x00
#define CMD_STATE_PROCESSING      0x10
#define CMD_STATE_DATA_LAST       0x20
#define CMD_STATE_DATA_MORE       0x30

#define HANDSHAKE_RESET           0x87
#define HANDSHAKE_ACCEPT_COMMAND  0x01
#define HANDSHAKE_ACCEPT_NEXTDATA 0x02
#define HANDSHAKE_ACCEPT_ABORT    0x04
#define HANDSHAKE_VALIDATE_LAST   0x10
#define HANDSHAKE_VALIDATE_MORE   0x30

#define CMD_HS_DMA_ACTIVE         0x80
#define CMD_HS_TRIGGER_ACTIVE     0x40

#define CMD_TARGET_NONE 0xFF

#define CMD_MAX_COMMAND_LEN 896
#define CMD_MAX_REPLY_LEN   896
#define CMD_MAX_STATUS_LEN  256

typedef struct _message
{
    int   length;
    bool  last_part;
    uint8_t *message;
} Message;

class SubSystem;

class CommandInterface : public SubSystem
{
	TaskHandle_t taskHandle;
	TaskHandle_t resetTaskHandle;

	int response_offset;
	uint8_t *command_buffer;
    uint8_t *response_buffer;
    uint8_t *status_buffer;

    Message incoming_command;
    uint8_t target;    
    void copy_result(Message *data, Message *status);
    int cart_mode;
    
    static void start_task(void *a);
    static void reset_task(void *a);
    void run_task(void);
    void run_reset_task(void);

public:
	QueueHandle_t queue; // needed from IRQ

	CommandInterface();
    ~CommandInterface();
    
    void dump_registers(void);
    const char *identify(void) { return "Command Interface"; }
    bool is_dma_active(void);
    void set_kernal_device_id(uint8_t id);
};

extern CommandInterface cmd_if;
 

/* Command Targets */

#define CMD_IF_MAX_TARGET   0x0F
#define CMD_IF_NO_REPLY     0x80

// Mandatory commands for all targets
#define CMD_IDENTIFY   0x01

extern Message c_message_no_target;
extern Message c_status_ok;
extern Message c_status_unknown_command;
extern Message c_message_empty;

class CommandTarget
{
public:
    CommandTarget() { }
    virtual ~CommandTarget() { }

    virtual void parse_command(Message *command, Message **reply, Message **status) {
        if(command->message[1] == CMD_IDENTIFY) {
            *reply  = &c_message_no_target;
            *status = &c_status_ok;
        } else {
            *reply  = &c_message_empty;
            *status = &c_status_unknown_command; 
        }        
    }
    virtual void get_more_data(Message **reply, Message **status) {
        *reply  = &c_message_empty;
        *status = &c_status_ok; 
    }
    
    virtual void abort(int a) { }
};

extern CommandTarget *command_targets[];


#endif