aboutsummaryrefslogtreecommitdiff
path: root/mubios/services/int14.c
blob: 4bc04a4bd7cb39220f39c453a0a11ecaa6e1fe54 (plain)
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
/*
 RCC-Project

 Copyright (C) 2026 Richard Qin

 This file is part of RCC-Project.

 RCC-Project 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.

 RCC-Project 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
 RCC-Project; see the file COPYING.  If not see
 <http://www.gnu.org/licenses/>.
 */

#include "bios.h"
#include "regs.h"

#define BIOS_CF 0x0001u
#define BIOS_INVALID_FUNCTION 0x86u
#define BDA_COM_PORTS 0x0400u
#define BDA_COM_TIMEOUTS 0x047Cu
#define BIOS_COM_PORT_COUNT 4u

void
int14_service (struct bios_regs *regs)
{
  u8 function;
  u8 status;
  u8 value;
  u8 timeout;
  u16 port;
  u16 base;
  u16 result;
  volatile u16 *ports;
  volatile u8 *timeouts;

  function = (u8)(regs->eax >> 8);
  port = (u16)regs->edx;
  ports = (volatile u16 *)BDA_COM_PORTS;
  timeouts = (volatile u8 *)BDA_COM_TIMEOUTS;

  /* BIOS numbers COM ports through DX and advertises their bases in BDA. */
  if (function > 0x03u || port >= BIOS_COM_PORT_COUNT || ports[port] == 0u)
    {
      regs->eax
          = (regs->eax & 0xFFFF00FFu) | ((u32)BIOS_INVALID_FUNCTION << 8);
      regs->flags |= BIOS_CF;
      return;
    }

  base = ports[port];
  timeout = timeouts[port];

  switch (function)
    {
    case 0x00u:
      result = serial_configure (base, (u8)regs->eax);
      regs->eax = (regs->eax & 0xFFFF0000u) | result;
      break;

    case 0x01u:
      status = serial_transmit (base, (u8)regs->eax, timeout);
      regs->eax = (regs->eax & 0xFFFF00FFu) | ((u32)status << 8);
      break;

    case 0x02u:
      value = (u8)regs->eax;
      status = serial_receive (base, &value, timeout);
      regs->eax = (regs->eax & 0xFFFF0000u) | ((u32)status << 8) | value;
      break;

    case 0x03u:
      result = serial_status (base);
      regs->eax = (regs->eax & 0xFFFF0000u) | result;
      break;
    }

  /* As in SeaBIOS, timeouts use AH bit 7; CF denotes an invalid call. */
  regs->flags &= (u16)~BIOS_CF;
}