SGL User's ManualPROGRAMMER'S STRUCT
BackForward
PROGRAMMER'S STRUCT

9. Controller input


In this chapter, we will explain the recognition method and actual operation of the Sega Saturn data input device using the Sega Saturn PAD, which is a typical Sega Saturn input device.
The Sega Saturn PAD has four direction keys, ABCXYZ buttons, an LR button on the top of the controller, and a start button as input devices.
We will explain how the data input using each input device of these controllers is judged inside Sega Saturn together with the sample program.

9-1. Input system used in Sega Saturn

Sega Saturn can receive data from various data input devices connected to the input port and reflect that data in the program.

The data input device mainly used in Sega Saturn is a control PAD attached to the main body called Sega Saturn PAD. Sega Saturn PAD is a direction key, start button, ABC button, XYZ arranged to point in four directions. It is an input device consisting of buttons and LR buttons.

Figure 9-1 Example of input device (Sega Saturn PAD)

● For Sega Saturn PAD, the following data can be entered.

In addition to the Sega Saturn PAD, Sega Saturn supports input devices as shown in Table 9-1.
However, please note that some of the devices in this table are not currently on sale (and will be available in the future).

Table 9-1 List of input devices
 Genus
 Name
 Input composition
      remarks
Digital Sega Saturn PAD Arrow keys, start, 8 buttons Sega Saturn standard pad
Sega Saturn Mouse XY movement amount, start, 3 buttons The amount of mouse movement is stored as an absolute value.
Megadora 3 button pad Arrow keys, start, 3 buttons Can be connected with Sega Tap
Megadora 6 button pad Arrow keys, start, 6 buttons Can be connected with Sega Tap
analog Analog joystick
(Mission stick)
Direction lever, start, 8 buttons The amount of movement is the absolute value of the unsigned A / D output.
Special Sega Saturn keyboard Compatible with IBM keyboards
Auxiliary device Sega Saturn 6P multi-tap Number of connects 6
Sega tap Number of connects 4 Used to connect the Megadora PAD
Note) For details on each input device, refer to the "HARDWARE MANUAL / SMPC User's Manual".

9-2. Actual operation

The input device connected to the input port sends a signal to the Sega Saturn main unit when the device's input device (direction keys, buttons, etc.) is turned on.
In response to this signal, Sega Sega Saturn changes the device bits provided in the system, and by reading this change, the programmer can read the state of the input device.
Therefore, in this chapter, we will explain the actual operation contents using Sega Saturn PAD, which is a typical input device of Sega Saturn.

Bits used in the input system

Devices connected to the input port (Sega Saturn PAD, etc.) are recognized by the system, and a certain area corresponding to each device is secured in the system.
This area contains the peripheral ID (8bit), which is the device type recognition bit string (unique to each device), and the input state bit string that follows it.
The input status bit string consists of one data string of 8 bits, and the data string required for the connected device is prepared.
The bit value of the input state bit string keeps 1 when there is no input, and changes to 0 only when there is an input (while the key or button is pressed).

Sega Saturn PAD is processed as a set of bits as follows.

Table 9-2 Sega Saturn PAD Peripheral Data Format
bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
Peripheral ID 0 0 0 0 0 0 1 0
1st DATA Start A C B
2nd DATA R X Y Z L 1 1 1
Note) The last 3 bits of 2nd DATA are 1 for convenience because they are unused.

reference:
For the peripheral data format of input devices other than Sega Saturn PAD,
"HARDWARE MANUAL" / "SMPC User's Manual" ■ 3.3 Support Peripheral Data Format
Please refer to.

About data format storage

A bit string representing the state of each device is automatically stored in the area specified by the system.

What is a peripheral?

Peripheral is a general term for all peripheral devices such as input and output, and peripheral ID is an ID for identifying peripheral devices.

Bit operation by input

Information about the device connected to the input port is reserved in the system area as a set of peripheral IDs followed by a string of data (indicating the input state of the device).
From now on, the state change of the device is expressed as the state change of the specified area, and the programmer can receive the information from the input device simply by collating the state of this area.

Figure 9-2 shows the pure device status information excluding the peripheral ID part of the Sega Saturn PAD device information extracted and expanded into a 16-bit long data string (data output by SGL as peripheral data). Is also expanded to this type).
Also, for devices other than Sega Saturn PAD, only this part is used for actual device processing (however, the data length differs depending on the device).
The input state bits are all represented by 1 when there is no input, and the bit corresponding to the input device changes to 0 only when there is an input (while the key or button is pressed).

Figure 9-2 Input state bit array of Sega Saturn PAD (16-bit display)

Figure 9-3 shows the change in the state bit string (16 bits) when there is sequential input from the state where there is no input in Sega Saturn PAD.

Figure 9-3 Changes in input state bit string (Sega Saturn PAD)

Handling of device information in SGL

In the case of SGL, the information of the Sega Saturn PAD connected to the input port is stored in the system variable "Smpc_Peripheral [n]" (n = 0 to 29).

The total number of devices currently connected to input port 1 and input port 2 is stored in the system variables “Per_Connect1” and “Per_Connect2”.
Also, when multi-tap is connected, the device information is stored in the area of “Smpc_Peripheral [n]” (n = 0 to 14, 15 to 29).

The system variable “Smpc_Peripheral []” stores a system-defined PerDigital type structure, and each member of the structure contains the information shown in the figure below.

Figure 9-4 Definition of “PerDigital” structure
● Definition of PerDigital structure ●

typedef struct {/ * digital device * /
	Uint8 id; / * Peripheral ID * /
	Uint16 data; / * Current peripheral data * /
	Uint16 push / * Pressed switch data * /
	Uint16 pull / * Data of released switch * /
} PerDigital;

Note) The above structure is defined in the header file “sl_def.h” attached to the system.

data: 16-bit long peripheral data (indicating the current bit state)
push: 16-bit long peripheral data (bits are changed only at the moment when input is executed)
pull: 16-bit long peripheral data (bits are changed only at the moment when input is released)
id: 8-bit data string indicating the device peripheral ID

Peripheral data is a set of bits that represent only the input state, excluding the peripheral ID, from the above-mentioned set of data. In the case of Sega Saturn PAD, the input state bits are expanded to 16 bits in length. ..

Peripheral data output

The members of the structure showing the data of the Sega Saturn PAD introduced here consist only of the peripheral ID (8bit) and the ON / OFF information (16bit) of the switch of the input device, but for devices other than the Sega Saturn PAD. The corresponding structures are not always the same.
For example, in the case of the Sega Saturn mouse, which is a pointing device, the peripheral data holds the X and Y coordinates of the mouse pointer in addition to the ON / OFF (16bit) of the switch as a digital device. However, since the size of the structure is the same for all devices, it is also possible to refer to the data of the Sega Saturn mouse as the Sega Saturn PAD type.

Note <BR> For details on peripherals, refer to "HARDWEAR MANUAL vol.1" and the header file "sl_def.h" attached to the system.

Discrimination of input data

The input information from the device is determined by collating the peripheral data described above with the assignment data described below.
The assignment data is defined in the header file "sl_def.h" attached to the system. For example, the assignment corresponding to each input device of Sega Saturn PAD is defined as shown in the following figure.

Figure 9-5 #define value of assignment data (Sega Saturn PAD)
● Pat assignment ●
#define PER_DGT_KR (1 << 15) / * Arrow keys (→) * /
#define PER_DGT_KL (1 << 14) / * Arrow keys (←) * /
#define PER_DGT_KD (1 << 13) / * Direction key (↓) * /
#define PER_DGT_KU (1 << 12) / * Arrow keys (↑) * /
#define PER_DGT_ST (1 << 11) / * Start button * /
#define PER_DGT_TA (1 << 10) / * A button * /
#define PER_DGT_TC (1 << 9) / * C button * /
#define PER_DGT_TB (1 << 8) / * B button * /
#define PER_DGT_TR (1 << 7) / * R trigger * /
#define PER_DGT_TX (1 << 6) / * X button * /
#define PER_DGT_TY (1 << 5) / * Y button * /
#define PER_DGT_TZ (1 << 4) / * Z button * /
#define PER_DGT_TL (1 << 3) / * L trigger * /

Since these assignments are used for collation with peripheral data, they correspond to the peripheral data length of each device. For example, the assignment for the A button of Sega Saturn PAD is expanded as shown in the following figure (16-bit length). In the assignment data, the bit corresponding to the input device to be discriminated is set to 1, and the other bits are set to 0.

Figure 9-6 Pad assignment details (for PER_DGT_TA)

Note) Only the same bit (10th bit) that operates when the A button is input is set to 1.

In the case of Sega Saturn PAD, the input device consists of a simple switch ON / OFF, so the switch status can be checked by logical operation of the assignment data and peripheral data.

Figure 9-7 is an example of checking the input status by AND operation of peripheral data and assignment data. If the input device specified by the assignment data is ON, 0 is output as the calculation result.

This method is also adopted in the sample program, and it is designed to display a sprite on the screen when the input device pointed to by the pad assignment is in the ON state.

Figure 9-7 Confirmation of input status using assignment data

Also, here, data collation was performed by AND operation of peripheral data and assignment data, but of course, it is not always necessary to adopt this method.

9-3. Sample program

The sample program (Listing 9-1) was created to test the controller input.
The program displays a graphic imitating the Sega Saturn PAD on the screen, and when there is a controller input, an instruction mark (finger shape) is displayed on the button at the position corresponding to the controller input.
In addition, this program uses sprites for instruction marks.

Listing 9-1 sample_9_1: Input test (main.c)


/ * ------------------------------------------------ ---------------------- * /
/ * Pad Control * /
/ * ------------------------------------------------ ---------------------- * /
#include "sgl.h"
#include "sega_sys.h"

#define NBG1_CEL_ADR (VDP2_VRAM_B1 + 0x02000)
#define NBG1_MAP_ADR (VDP2_VRAM_B1 + 0x12000)
#define NBG1_COL_ADR (VDP2_COLRAM + 0x00200)
#define BACK_COL_ADR (VDP2_VRAM_A1 + 0x1fffe)
#define PAD_NUM 13

static Uint16 pad_asign [] = {
	PER_DGT_KU,
	PER_DGT_KD,
	PER_DGT_KR,
	PER_DGT_KL,
	PER_DGT_TA,
	PER_DGT_TB,
	PER_DGT_TC,
	PER_DGT_ST,
	PER_DGT_TX,
	PER_DGT_TY,
	PER_DGT_TZ,
	PER_DGT_TR,
	PER_DGT_TL,
};

extern pad_cel [];
extern pad_map [];
extern pad_pal [];
extern TEXTURE tex_spr [];
extern PICTURE pic_spr [];
extern FIXED stat [] [XYZS];
extern SPR_ATTR attr [];
extern ANGLE angz [];

static void set_sprite (PICTURE * pcptr, Uint32 NbPicture)
{
	TEXTURE * txptr;
 
	for (; NbPicture-> 0; pcptr ++) {
		txptr = tex_spr + pcptr-> texno;
		slDMACopy ((void *) pcptr-> pcsrc,
			(void *) (SpriteVRAM + ((txptr-> CGadr) << 3)),
			(Uint32) ((txptr-> Hsize * txptr-> Vsize * 4) >> (pcptr-> cmode)));
	}
}

static void disp_sprite ()
{
	static Sint32 i;
	Uint16 data;

	if (! Per_Connect1) return;
	data = Smpc_Peripheral [0] .data;

	for (i = 0; i <PAD_NUM; i ++) {
		if ((data & pad_asign [i]) == 0) {
			slDispSprite ((FIXED *) stat [i],
				(SPR_ATTR *) (& attr [i] .texno), (ANGLE) angz [i]);
		}
	}
}

void ss_main (void)
{
	slInitSystem (TV_320x224, tex_spr, 1);
	slTVOff ();
	set_sprite (pic_spr, 1);
	slPrint ("Sample program 9.1", slLocate (9,2));
	
	slColRAMMode (CRM16_1024);
	slBack1ColSet ((void *) BACK_COL_ADR, 0);

	slCharNbg1 (COL_TYPE_256, CHAR_SIZE_1x1);
	slPageNbg1 ((void *) NBG1_CEL_ADR, 0, PNB_1WORD | CN_12BIT);
	slPlaneNbg1 (PL_SIZE_1x1);
	slMapNbg1 ((void *) NBG1_MAP_ADR, (void *) NBG1_MAP_ADR, (void *) NBG1_MAP_ADR, (void *) NBG1_MAP_ADR);
	Cel2VRAM (pad_cel, (void *) NBG1_CEL_ADR, 483 * 64);
	Map2VRAM (pad_map, (void *) NBG1_MAP_ADR, 32, 19, 1, 256);
	Pal2CRAM (pad_pal, (void *) NBG1_COL_ADR, 256);

	slScrPosNbg1 (toFIXED (-32.0), toFIXED (-36.0));
	slScrAutoDisp (NBG0ON | NBG1ON);
	slTVOn ();
	
	while (1) {
		disp_sprite ();
		slSynch ();
	} 
}

9-4. Library functions used in the sample

Here, we will briefly introduce some of the library functions used in the sample program that are not explained in the previous chapters and are considered to be particularly important. At the same time, I will also introduce some functions related to those functions.

Sprite function

A function for displaying sprites.
Unlike scrolling, sprites are graphic data types that are used when you want to freely move and display multiple objects in smaller units.
Sprites are often used for moving characters in games that use 2D graphics.

[Void slDispSprite (FIXED * pos, ATTR * atrb, ANGLE Zrot);]
Display the sprite on the screen by specifying the position, scale, and display angle.
Substitute the pointer of the 4-dimensional array FIXED type variable representing the XYZ coordinate value and scale value, the start address of the area where the sprite data is stored, and the display angle into the parameters.
Sorting by Z value is also executed like the library function “slPutPolygon”, but it is not affected by the current matrix at all.
If a negative value is assigned to the scale value, the scale is calculated based on the Z position, and then multiplied by the complement of the scale value of the parameter to obtain the display scale.
For example, when -2.0 is specified for the scale value, if there is a sprite at a position where it is displayed at 0.5 times, it is actually displayed at 1.0 times scale.

[Void slPutSprite (FIXED * pos, ATTR * atrb, ANGLE Zrot);]
The position is calculated using the current matrix and the sprite scaled according to the perspective transformation is displayed on the screen. Substitute the pointer of the 4-dimensional array FIXED type variable representing the XYZ coordinate value and scale value, the start address of the area where the sprite data is stored, and the display angle into the parameters. Similar to “slDispSprite”, sprites are scaled at the specified scale value, but if you assign a negative value, the sprites will be displayed on the screen flipped vertically and horizontally.

[Void slSetSprite (SPRITE * parms, FIXED Zpos);]
Sets the sprite control command data to be passed to the hardware in the transfer list. Substitute the start address and Z coordinate position of the area where the sprite data is stored in the parameters. Use this function if you want to set a modified sprite that cannot be created by a library function, or if you want to set a window that affects only a specific sprite.

Other functions

Both of the two functions introduced here are related to DMA transfer.
In the sample program, the function "slDMACopy" is used to transfer the picture data for sprites (data transfer from the picture data storage area to the character generator area).

[Void slDMACopy (void * src, void * dst, Uint32 cnt);]
Data block transfer is performed using the DMA built into the CPU.
Substitute the start address of the transfer source memory area, the start address of the transfer destination memory area, and the block transfer amount (unit: bytes) into the parameters.
This function ends the transfer immediately after the DMA starts, so if you want to know the transfer completion, use “slDMAWait”.

[Void slDMAWait (void);]
Wait for the end of DMA transfer started by “slDMACopy”.
“SlDMACopy” always transfers data using the same channel, but if this function is used, if the DMA transfer executed earlier is being executed, it will wait for the end of the transfer, and after the transfer is completed, the next data transfer will be performed. It will be started.

[Void slDMAXCopy (void * src, void * dst, Uint32 cnt, Uint16 mode);]
This allows you to specify how to move the pointer of the library function "slDMACopy". Substitute the transfer start address, block transfer amount (unit: bytes), and transfer mode flag of the transfer source memory area for the parameters. The transfer mode flag specifies what to do with the address of the transfer source memory area, what to do with the address of the transfer destination memory area, and what to do with the transfer unit. The specification method is as follows.

Figure 9-8 Parameter assignment value (mode) of “slDMAXCopy”
Forwarding destination
Increment Decrement Fixed
Forwarding source Increment Sinc_Dinc_Byte Sinc_Ddec_Byte Sinc_Dfix_Byte
Decrement Sdec_Dinc_Byte Ban Sdec_Dfix_Byte
Fixed Sfix_Dinc_Byte Sfix_Ddec_Byte Ban
Note) The above values are defined in "sl_def.h".
Note) The above is the value when the transfer unit is "Byte". You can specify "Word" and "Long" as other transfer units.

Appendix SGL library functions that appeared in this chapter

In this chapter, the functions in the following table are explained.

Table 9-3 Library functions introduced in this chapter
 Functional type
 Seki several people
 Parameter
         function
void slDispSprite FIXED * pos, ATTR * atrb, ANGLE Zrot Sprite display by specifying the position, scale, and display angle
void slPutSprite FIXED * pos, ATTR * atrb, ANGLE Zrot Sprite display according to perspective conversion
void slSetSprite SPRITE * parms, FIXED Zpos Set in a buffer to transfer sprite data to hardware
void slDMACopy void * src, void * dst, Uint32 cnt DMA transfer only C from A to B (same bus transfer possible)
void slDMAWait void Wait until the DMA transfer ends
void slDMAXCopy void * src, void * dst, Uint32 cnt, Uint16 mode DMA transfer from A to B in C only D transfer mode (same bus transfer possible)


BackForward
SGL User's ManualPROGRAMMER'S STRUCT
Copyright SEGA ENTERPRISES, LTD., 1997