~lgb/x-emulators/dev

« back to all changes in this revision

Viewing changes to targets/ep128/w5300.c

  • Committer: GitHub
  • Author(s): LGB
  • Date: 2020-06-09 11:33:47 UTC
  • mfrom: (265.1.101)
  • Revision ID: git-v1:527c2b021966f6f9f2444c23d6402c141d1c19d2
Merge pull request #116 from lgblgblgb/dev

Merge dev stage into master

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Xep128: Minimalistic Enterprise-128 emulator with focus on "exotic" hardware
2
 
   Copyright (C)2015,2016 LGB (Gábor Lénárt) <lgblgblgb@gmail.com>
3
 
   http://xep128.lgb.hu/
4
 
 
5
 
   Partial Wiznet W5300 emulation, using the host OS (which runs the emulator)
6
 
   TCP/IP API. Thus, many of the W5300 features won't work, or limited, like:
7
 
   RAW mode, ICMP sockets, listening mode (it would be possible but eg in case
8
 
   of UNIX there would be a need for privilege, which is not so nice to
9
 
   run an emulator as "root" user), no IP/MAC setting (always uses the OS IP
10
 
   and MAC). DHCP and DNS is planned to "faked" so w5300 softwares trying to
11
 
   get IP address via DHCP or wanting resolve a DNS name would get an answer
12
 
   from this w5300 emulator instead of from the "real" network.
13
 
 
14
 
   Note: I've just discovered that FUSE Spectrum emulator does have some kind
15
 
   of w5100 emulation. Though w5100 and w5300 are not the very same, some things
16
 
   are similar, so their sources may help me in the future, thanks for their
17
 
   work (also a GNU/GPL software, so there is no license problem here).
18
 
 
19
 
This program is free software; you can redistribute it and/or modify
20
 
it under the terms of the GNU General Public License as published by
21
 
the Free Software Foundation; either version 2 of the License, or
22
 
(at your option) any later version.
23
 
 
24
 
This program is distributed in the hope that it will be useful,
25
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 
GNU General Public License for more details.
28
 
 
29
 
You should have received a copy of the GNU General Public License
30
 
along with this program; if not, write to the Free Software
31
 
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
32
 
 
33
 
#include "xep128.h"
34
 
#include "w5300.h"
35
 
 
36
 
 
37
 
 
38
 
#ifdef CONFIG_W5300_SUPPORT
39
 
 
40
 
int w5300_int;
41
 
 
42
 
//static Uint8 wmem[0x20000];   // 128K of internal RAM of w5300
43
 
static Uint8 wregs[0x400];      // W5300 registers
44
 
static Uint8 idm_ar0, idm_ar1, idm_ar;
45
 
static Uint8 mr0, mr1;
46
 
static void (*interrupt_cb)(int);
47
 
 
48
 
 
49
 
static void update_interrupts ( void )
50
 
{
51
 
        int v = ((wregs[2] & wregs[4] & 0xF0) | (wregs[3] & wregs[5])) > 0;
52
 
        if (v != w5300_int) {
53
 
                w5300_int = v;
54
 
                interrupt_cb(v);
55
 
        }
56
 
}
57
 
 
58
 
 
59
 
 
60
 
static Uint8 read_reg ( int addr )
61
 
{
62
 
        return wregs[addr];
63
 
}
64
 
 
65
 
static void write_reg ( int addr, Uint8 data )
66
 
{
67
 
        switch (addr) {
68
 
                case 2: // IR0
69
 
                case 3: // IR1
70
 
                        wregs[addr] &= 255 - data;
71
 
                        update_interrupts();
72
 
                        break;
73
 
                case 4: // IMR0
74
 
                case 5: // IMR1
75
 
                        wregs[addr] = data;
76
 
                        update_interrupts();
77
 
                        break;
78
 
                default:
79
 
                        wregs[addr] = data;
80
 
                        break;
81
 
        }
82
 
}
83
 
 
84
 
 
85
 
static void default_interrupt_callback ( int level ) {
86
 
}
87
 
 
88
 
 
89
 
/* ---Interface functions --- */
90
 
 
91
 
 
92
 
void w5300_reset ( void )
93
 
{
94
 
        memset(wregs, 0, sizeof wregs);
95
 
        mr0 = 0x38; mr1 = 0x00;
96
 
        idm_ar0 = 0; idm_ar1 = 0; idm_ar = 0;
97
 
        w5300_int = 0;
98
 
        wregs[0x1C] = 0x07; wregs[0x1D] = 0xD0; // RTR retransmission timeout-period register
99
 
        wregs[0x1F] = 8;                        // RCR retransmission retry-count register
100
 
        memset(wregs + 0x20, 8, 16);            // TX and RX mem size conf
101
 
        wregs[0x31] = 0xFF;                     // MTYPER1
102
 
        DEBUG("W5300: reset" NL);
103
 
}
104
 
 
105
 
void w5300_init ( void (*cb)(int) )
106
 
{
107
 
        interrupt_cb = cb ? cb : default_interrupt_callback;
108
 
        DEBUG("W5300: init" NL);
109
 
}
110
 
 
111
 
void w5300_shutdown ( void )
112
 
{
113
 
        DEBUG("W5300: shutdown pending connections (if any)" NL);
114
 
}
115
 
 
116
 
void w5300_write_mr0 ( Uint8 data ) {           // high byte of MR
117
 
        if (data & 1) ERROR_WINDOW("W5300: FIFO byte-order swap feature is not emulated");
118
 
        mr0 = data & 0x3F; // DBW and MPF bits cannot be overwritten by user
119
 
}
120
 
void w5300_write_mr1 ( Uint8 data ) {           // low byte of MR
121
 
        if (data & 128) { // software reset?
122
 
                w5300_reset();
123
 
                w5300_shutdown();       // shuts down host OS connections, etc, emulator is being done
124
 
        } else {
125
 
                if (data & 8) ERROR_WINDOW("W5300: PPPoE mode is not emulated");
126
 
                if (data & 4) ERROR_WINDOW("W5300: data bus byte-order swap feature is not emulated");
127
 
                if ((data & 1) == 0) ERROR_WINDOW("W5300: direct mode is NOT emulated, only indirect");
128
 
                mr1 = data;
129
 
        }
130
 
}
131
 
void w5300_write_idm_ar0 ( Uint8 data ) {       // high byte of address
132
 
        idm_ar0 = data;
133
 
        idm_ar = (idm_ar & 0xFF) | ((data & 0x3F) << 8);
134
 
}
135
 
void w5300_write_idm_ar1 ( Uint8 data ) {       // low byte of address
136
 
        idm_ar1 = data;
137
 
        idm_ar = (idm_ar & 0xFF00) | (data & 0xFE);
138
 
}
139
 
void w5300_write_idm_dr0 ( Uint8 data ) {       // high byte of adta
140
 
        write_reg(idm_ar, data);
141
 
}
142
 
void w5300_write_idm_dr1 ( Uint8 data ) {       // low byte of data
143
 
        write_reg(idm_ar | 1, data);
144
 
}
145
 
Uint8 w5300_read_mr0 ( void ) {
146
 
        return mr0;
147
 
}
148
 
Uint8 w5300_read_mr1 ( void ) {
149
 
        return mr1;
150
 
}
151
 
Uint8 w5300_read_idm_ar0 ( void ) {
152
 
        return idm_ar0;
153
 
}
154
 
Uint8 w5300_read_idm_ar1 ( void ) {
155
 
        return idm_ar1;
156
 
}
157
 
Uint8 w5300_read_idm_dr0 ( void ) {
158
 
        return read_reg(idm_ar);
159
 
}
160
 
Uint8 w5300_read_idm_dr1 ( void ) {
161
 
        return read_reg(idm_ar | 1);
162
 
}
163
 
 
164
 
#endif
165