aboutsummaryrefslogtreecommitdiff
path: root/src/drv/ps1/sio/src/write.c
blob: 942456ff5a9c4caa3a6f87c6f132575261638bda (plain) (blame)
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
/*
 * wnix, a Unix-like operating system for WebAssembly applications.
 * Copyright (C) 2025  Xavier Del Campo Romero
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <drv/ps1/sio/ops.h>
#include <drv/ps1/sio/routines.h>
#include <drv/ps1/sio/types.h>
#include <drv/ps1/bios.h>
#include <drv/event.h>
#include <kprintf.h>
#include <sys/types.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>

static int store(void);

static void write_fifo(struct drv_ps1_sio *const s)
{
    struct sio_fifo *const f = &s->tx;
    struct sio_req *const r = f->head;
    struct sio_req_w *const w = &r->u.w;
    const char *p = w->buf;

    while (w->n)
    {
        size_t n = f->pend + 1;

        if (n >= sizeof f->buf)
            n = 0;
        else if (n == f->proc)
            break;

        f->buf[f->pend = n] = *p++;
        w->buf = p;
        w->n--;
    }
}

static int check(void)
{
    struct drv_ps1_sio *const s = &drv_ps1_sio;
    struct sio_fifo *const f = &s->tx;
    struct sio_req *const r = f->head;
    struct sio_req_w *const w = &r->u.w;

    if (!w->n)
    {
        const struct drv_event_done *const d = &r->done;

        if (d->f && d->f(SUCCESS, d->args))
            return -1;

        return drv_ps1_sio_next(f);
    }

    f->next = store;
    return 0;
}

static int store(void)
{
    struct drv_ps1_sio *const s = &drv_ps1_sio;
    struct sio_fifo *const f = &s->tx;
    struct sio_req_w *const w = &f->head->u.w;

    write_fifo(s);

    if (!w->n)
        f->next = check;

    return 0;
}

int drv_ps1_sio_write(const void *const buf, const size_t n,
    const struct drv_event_done *const done, void *const args)
{
    struct drv_ps1_sio *const s = &drv_ps1_sio;
    struct sio_fifo *const f = &s->tx;
    struct sio_req *const req = malloc(sizeof *req);

    if (!req)
        return -1;

    *req = (const struct sio_req)
    {
        .f = store,
        .done = *done,
        .u.w =
        {
            .buf = buf,
            .n = n
        }
    };

    if (!f->head)
        f->head = req;
    else
        f->tail->next = req;

    f->tail = req;
    return 0;
}