summaryrefslogtreecommitdiff
path: root/apps/codecs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs')
-rw-r--r--apps/codecs/libmad/imdct_mcf5249.S12
1 files changed, 5 insertions, 7 deletions
diff --git a/apps/codecs/libmad/imdct_mcf5249.S b/apps/codecs/libmad/imdct_mcf5249.S
index d6d4460..b5e2a12 100644
--- a/apps/codecs/libmad/imdct_mcf5249.S
+++ b/apps/codecs/libmad/imdct_mcf5249.S
@@ -34,10 +34,10 @@ III_imdct_s:
* get more low bits out of the accext01 register _before_ doing the
* movclrs.
*/
- sub.l %a0, %a0 /* clear loop variable */
+ moveq.l #3, %d5 /* we do three outer loop iterations */
.imdctloop: /* outer loop label */
lea.l imdct_s, %a1 /* load pointer to imdct coefs in a1 */
- movem.l (%a2), %d0-%d5 /* load some input data in d0-d5 */
+ movem.l (%a2), %d0-%d4/%a0 /* load some input data in d0-d4/a0 */
lea.l (6*4, %a2), %a2
clr.l %d7 /* clear loop variable */
@@ -48,7 +48,7 @@ III_imdct_s:
mac.l %d2, %a4, (%a1)+, %a4, %acc0
mac.l %d3, %a4, (%a1)+, %a4, %acc0
mac.l %d4, %a4, (%a1)+, %a4, %acc0
- mac.l %d5, %a4, (%a1)+, %a4, %acc0
+ mac.l %a0, %a4, (%a1)+, %a4, %acc0
movclr.l %acc0, %d6 /* get result, left shifted once */
asl.l #3, %d6 /* one shift free, shift three more */
move.l %d6, (%a3, %d7.l*4) /* yptr[i] = result */
@@ -60,7 +60,7 @@ III_imdct_s:
mac.l %d2, %a4, (%a1)+, %a4, %acc0
mac.l %d3, %a4, (%a1)+, %a4, %acc0
mac.l %d4, %a4, (%a1)+, %a4, %acc0
- mac.l %d5, %a4, (%a1)+, %a4, %acc0
+ mac.l %a0, %a4, (%a1)+, %a4, %acc0
movclr.l %acc0, %d6 /* get result */
asl.l #3, %d6
move.l %d6, (11*4, %a3, %d7.l*4) /* yptr[11 - i] = result */
@@ -72,9 +72,7 @@ III_imdct_s:
jne .macloop
lea.l (12*4, %a3), %a3 /* add pointer increment */
- addq.l #1, %a0 /* increment outer loop variable */
- moveq.l #3, %d0
- cmp.l %d0, %a0 /* we do three outer loop iterations */
+ subq.l #1, %d5 /* decrement outer loop variable */
jne .imdctloop
/* windowing, overlapping and concatenation */
ref='#n172'>172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
/*
 *   NetCosm - a MUD server
 *   Copyright (C) 2016 Franklin Wei
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU 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 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 <http://www.gnu.org/licenses/>.
 */

#include "globals.h"

#include "hash.h"
#include "server.h"
#include "userdb.h"

/* sends a single packet to a child, virtually guarantees receipt */
static void send_packet(struct child_data *child, unsigned char cmd,
                        void *data, size_t datalen)
{
    assert(datalen < MSG_MAX);
    unsigned char pkt[MSG_MAX];
    pkt[0] = cmd;

    //if((data?datalen:0) + 1 > MSG_MAX && cmd == REQ_BCASTMSG)
    //{
    //    /* TODO: split long messages */
    //    ;
    //}

    if(data && datalen)
        memcpy(pkt + 1, data, datalen);
tryagain:
    if(write(child->outpipe[1], pkt, (data?datalen:0) + 1) < 0)
    {
        /* write can fail, so we try again */
        if(errno == EAGAIN)
            goto tryagain;
    }
}

static void req_pass_msg(unsigned char *data, size_t datalen,
                         struct child_data *sender, struct child_data *child)
{
    (void) sender;

    send_packet(child, REQ_BCASTMSG, data, datalen);
    if(child->pid != sender->pid)
        send_packet(child, REQ_ALLDONE, NULL, 0);
}

static void req_send_clientinfo(unsigned char *data, size_t datalen,
                                struct child_data *sender, struct child_data *child)
{
    (void) data;
    (void) datalen;
    char buf[128];
    const char *state[] = {
        "INIT",
        "LOGIN SCREEN",
        "CHECKING CREDENTIALS",
        "LOGGED IN AS USER",
        "LOGGED IN AS ADMIN",
        "ACCESS DENIED",
    };

    if(child->user)
        snprintf(buf, sizeof(buf), "Client %s PID %d [%s] USER %s",
                 inet_ntoa(child->addr), child->pid, state[child->state], child->user);
    else
        snprintf(buf, sizeof(buf), "Client %s PID %d [%s]",
                 inet_ntoa(child->addr), child->pid, state[child->state]);

    if(sender->pid == child->pid)
        strncat(buf, " [YOU]\n", sizeof(buf) - strlen(buf) - 1);
    else
        strncat(buf, "\n", sizeof(buf) - strlen(buf) - 1);

    send_packet(sender, REQ_BCASTMSG, buf, strlen(buf));
}

static void req_change_state(unsigned char *data, size_t datalen,
                             struct child_data *sender, struct child_data *child)
{
    (void) data; (void) datalen; (void) child; (void) sender;
    if(datalen == sizeof(sender->state))
        sender->state = *((int*)data);
}

static void req_change_user(unsigned char *data, size_t datalen,
                            struct child_data *sender, struct child_data *child)
{
    (void) data; (void) datalen; (void) child; (void) sender;
    if(sender->user)
        free(sender->user);
    sender->user = strdup((char*)data);
}

//void req_hang(unsigned char *data, size_t datalen,
//              struct child_data *sender, struct child_data *child)
//{
//    while(1);
//}

static void req_kick_client(unsigned char *data, size_t datalen,
                            struct child_data *sender, struct child_data *child)
{
    /* format is | PID | Message | */
    (void) data; (void) datalen; (void) child; (void) sender;
    if(datalen >= sizeof(pid_t))
    {
        pid_t kicked_pid = *((pid_t*)data);
        if(kicked_pid == child->pid)
            send_packet(child, REQ_KICK, data + sizeof(pid_t), datalen - sizeof(pid_t));
    }
}

static void req_wait(unsigned char *data, size_t datalen, struct child_data *sender)
{
    (void) data; (void) datalen; (void) sender;
    sleep(10);
}

static void req_send_desc(unsigned char *data, size_t datalen, struct child_data *sender)
{
    (void) data; (void) datalen; (void) sender;
    struct room_t *room = room_get(sender->room);
    send_packet(sender, REQ_BCASTMSG, room->data.desc, strlen(room->data.desc));


    send_packet(sender, REQ_PRINTNEWLINE, NULL, 0);
}

static void req_send_roomname(unsigned char *data, size_t datalen, struct child_data *sender)
{
    (void) data; (void) datalen; (void) sender;
    struct room_t *room = room_get(sender->room);
    send_packet(sender, REQ_BCASTMSG, room->data.name, strlen(room->data.name));

    send_packet(sender, REQ_PRINTNEWLINE, NULL, 0);
}

static void child_set_room(struct child_data *child, room_id id)
{
    child->room = id;
    room_user_add(id, child);
}

static void req_set_room(unsigned char *data, size_t datalen, struct child_data *sender)
{
    (void) data; (void) datalen; (void) sender;
    room_id id = *((room_id*)data);

    child_set_room(sender, id);
}

static void req_move_room(unsigned char *data, size_t datalen, struct child_data *sender)
{
    (void) data; (void) datalen; (void) sender;
    enum direction_t dir = *((enum direction_t*)data);
    struct room_t *current = room_get(sender->room);

    room_user_del(sender->room, sender);

    /* TODO: checking */
    debugf("Moving in direction %d\n", dir);
    room_id new = current->adjacent[dir];
    int status = 0;
    if(new != ROOM_NONE)
    {
        child_set_room(sender, new);
        status = 1;
    }
    debugf("server status: %d\n", status);

    send_packet(sender, REQ_MOVE, &status, sizeof(status));
}

static void req_send_user(unsigned char *data, size_t datalen, struct child_data *sender)
{
    if(datalen)
    {
        struct userdata_t *user = userdb_lookup((char*)data);

        if(user)
        {
            send_packet(sender, REQ_GETUSERDATA, user, sizeof(*user));
            return;
        }

        debugf("looking up user %s failed\n", data);
    }
}

static void req_del_user(unsigned char *data, size_t datalen, struct child_data *sender)
{
    bool success = false;
    if(datalen)
    {
        success = userdb_remove((char*)data);
    }
    send_packet(sender, REQ_DELUSERDATA, &success, sizeof(success));
}

static void req_add_user(unsigned char *data, size_t datalen, struct child_data *sender)
{
    bool success = false;
    if(datalen == sizeof(struct userdata_t))
    {
        success = userdb_add((struct userdata_t*)data);
    }
    send_packet(sender, REQ_ADDUSERDATA, &success, sizeof(success));
}

static void req_send_geninfo(unsigned char *data, size_t datalen, struct child_data *sender)
{
    (void) data;
    (void) datalen;
    char buf[128];
    int len = snprintf(buf, sizeof(buf), "Total clients: %d\n", num_clients);
    send_packet(sender, REQ_BCASTMSG, buf, len);
}

static void req_kick_always(unsigned char *data, size_t datalen,
                            struct child_data *sender, struct child_data *child)
{
    (void) sender;
    send_packet(child, REQ_KICK, data, datalen);
}

static const struct child_request {
    unsigned char code;

    bool havedata;

    enum { CHILD_NONE, CHILD_SENDER, CHILD_ALL_BUT_SENDER, CHILD_ALL } which;

    /* sender_pipe is the pipe to the sender of the request */
    /* data points to bogus if havedata = false */
    void (*handle_child)(unsigned char *data, size_t len,
                         struct child_data *sender, struct child_data *child);

    void (*finalize)(unsigned char *data, size_t len, struct child_data *sender);
} requests[] = {
    { REQ_NOP,         false, CHILD_NONE,           NULL,                NULL,              },
    { REQ_BCASTMSG,    true,  CHILD_ALL,            req_pass_msg,        NULL,              },
    { REQ_LISTCLIENTS, false, CHILD_ALL,            req_send_clientinfo, req_send_geninfo,  },
    { REQ_CHANGESTATE, true,  CHILD_SENDER,         req_change_state,    NULL,              },
    { REQ_CHANGEUSER,  true,  CHILD_SENDER,         req_change_user,     NULL,              },
    { REQ_KICK,        true,  CHILD_ALL,            req_kick_client,     NULL,              },
    { REQ_WAIT,        false, CHILD_NONE,           NULL,                req_wait,          },
    { REQ_GETROOMDESC, false, CHILD_NONE,           NULL,                req_send_desc,     },
    { REQ_GETROOMNAME, false, CHILD_NONE,           NULL,                req_send_roomname, },
    { REQ_SETROOM,     true,  CHILD_NONE,           NULL,                req_set_room,      },
    { REQ_MOVE,        true,  CHILD_NONE,           NULL,                req_move_room,     },
    { REQ_GETUSERDATA, true,  CHILD_NONE,           NULL,                req_send_user,     },
    { REQ_DELUSERDATA, true,  CHILD_NONE,           NULL,                req_del_user,      },
    { REQ_ADDUSERDATA, true,  CHILD_NONE,           NULL,                req_add_user,      },
    { REQ_KICKALL,     true,  CHILD_ALL_BUT_SENDER, req_kick_always,     NULL               },
    //{ REQ_ROOMMSG,     true,  CHILD_ALL,            req_send_room_msg,   NULL,           },
};

static SIMP_HASH(unsigned char, uchar_hash);
static SIMP_EQUAL(unsigned char, uchar_equal);

static void *request_map = NULL;

void reqmap_init(void)
{
    request_map = hash_init(ARRAYLEN(requests), uchar_hash, uchar_equal);
    for(unsigned i = 0; i < ARRAYLEN(requests); ++i)
        hash_insert(request_map, &requests[i].code, requests + i);
}

void reqmap_free(void)
{
    if(request_map)
    {
        hash_free(request_map);
        request_map = NULL;
    }
}

/**
 * Here's how child-parent requests work
 * 1. Child writes its PID and length of request to the parent's pipe, followed
 *    by up to MSG_MAX bytes of data. If the length exceeds MSG_MAX bytes, the
 *    request will be ignored.
 * 1.1 Child spins until parent response.
 * 2. Parent handles the request.
 * 3. Parent writes its PID and length of message back to the child(ren).
 * 4. Parent signals child(ren) with SIGRTMIN
 * 5. Child(ren) handle parent's message.
 * 6. Child(ren) send the parent SIGRTMIN+1 to acknowledge receipt of message.
 * 7. Parent spins until the needed number of signals is reached.
 */

static unsigned char packet[MSG_MAX + 1];

bool handle_child_req(int in_fd)
{
    ssize_t packet_len = read(in_fd, packet, MSG_MAX);

    if((size_t)packet_len < sizeof(pid_t) + 1)
    {
        /* the pipe is probably broken (i.e. disconnect), so we don't
         * try to send a reply */
        return false;
    }

    struct child_data *sender = NULL;

    pid_t sender_pid;
    memcpy(&sender_pid, packet, sizeof(pid_t));

    sender = hash_lookup(child_map, &sender_pid);

    if(!sender)
    {
        debugf("WARNING: got data from unknown PID, ignoring.\n");
        goto fail;